关于java文件操作的算法的一个问题
我从页面传进来一个文件的路径,我想把它保存在另外的路径,但我对io操作不是很熟悉,想让大家帮我写完这个小方法/***保存文件方法*@paramimage文件路径*@par...
我从页面传进来一个文件的路径,我想把它保存在另外的路径,但我对io操作不是很熟悉,想让大家帮我写完这个小方法
/**
* 保存文件方法
* @param image 文件路径
* @param url 要保存的路径
* @throws FileNotFoundException
*/
public void saveFile(String image,String url) throws FileNotFoundException{
File file=new File(image);
FileOutputStream fos=new FileOutputStream(url);
InputStream is=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(is);
if(file.exists()){
}
}
谢谢大家了 展开
/**
* 保存文件方法
* @param image 文件路径
* @param url 要保存的路径
* @throws FileNotFoundException
*/
public void saveFile(String image,String url) throws FileNotFoundException{
File file=new File(image);
FileOutputStream fos=new FileOutputStream(url);
InputStream is=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(is);
if(file.exists()){
}
}
谢谢大家了 展开
5个回答
展开全部
package File;
import java.io.*;
/**
* @author songml
*
*/
public class FileOut {
/**
* 保存文件方法
*
* @param image
* 文件路径
* @param url
* 要保存的路径
* @throws IOException
*/
public static void saveFile(String image, String url) throws IOException {
File src = new File(image);
File dst = new File(url);
if (src.exists()) {
copy(src, dst);
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
saveFile("D:\\1.txt", "E:\\123.txt");
System.out.println("D:\\1.txt --- >>> E:\\123.txt copy");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Copies src file to dst file.
// If the dst file does not exist, it is created
static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
import java.io.*;
/**
* @author songml
*
*/
public class FileOut {
/**
* 保存文件方法
*
* @param image
* 文件路径
* @param url
* 要保存的路径
* @throws IOException
*/
public static void saveFile(String image, String url) throws IOException {
File src = new File(image);
File dst = new File(url);
if (src.exists()) {
copy(src, dst);
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
saveFile("D:\\1.txt", "E:\\123.txt");
System.out.println("D:\\1.txt --- >>> E:\\123.txt copy");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Copies src file to dst file.
// If the dst file does not exist, it is created
static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void saveFile(String image,String url) throws FileNotFoundException{
File file=new File(image);
File file1 = new File(url);
String message = null;
try{
FileReader reader = new FileReader(file);
FileWriter writer = new FileWriter(file1,true);
BufferedReader reader1 = new BufferedReader(reader);
if(file.exists())
{
while((message=reader1.readLine())!=null)
{
writer.write(message+"\r\n");
}
}
reader.close();
reader1.close();
writer.close();
}catch(Exception e){}
}
File file=new File(image);
File file1 = new File(url);
String message = null;
try{
FileReader reader = new FileReader(file);
FileWriter writer = new FileWriter(file1,true);
BufferedReader reader1 = new BufferedReader(reader);
if(file.exists())
{
while((message=reader1.readLine())!=null)
{
writer.write(message+"\r\n");
}
}
reader.close();
reader1.close();
writer.close();
}catch(Exception e){}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void saveFile(String image,String url) throws FileNotFoundException{
File file=new File(image);
FileOutputStream fos=new FileOutputStream(url);
InputStream is=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(is);
byte[] b = new byte[256];
int iRealNum = 0;
if(file.exists()){
while(bis.read(b) != -1) {
iRealNum = bis.read(b);
fos.write(b,0,iRealNum);
}
fos.close();
bis.close();
}
}
File file=new File(image);
FileOutputStream fos=new FileOutputStream(url);
InputStream is=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(is);
byte[] b = new byte[256];
int iRealNum = 0;
if(file.exists()){
while(bis.read(b) != -1) {
iRealNum = bis.read(b);
fos.write(b,0,iRealNum);
}
fos.close();
bis.close();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
OutputStream streamout = new FileOutputStream(dir + "\\" +storeFileName); //windows路径
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
来晚了。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询