//比如没关闭流之类的问题暂时不管.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class FileCopy { public static void main(String[] args) {
// TODO Auto-generated method stub
String oldPath = "F:\\test\\test1.txt";
String newPath = "F:\\test2\\";
Copy(oldPath,newPath);
} private static void Copy(String oldPath, String newPath) {
// TODO Auto-generated method stub
BufferedReader br = null;
BufferedWriter bw = null;
String str ="";
String fileName = "";
File oldFile = new File(oldPath);
fileName = oldFile.getName();
if(oldFile.exists()){
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(oldPath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("地址" + oldPath + "不存在文件");
e.printStackTrace();
}
}
File newFile = new File(newPath);
if(!newFile.exists()){
newFile.mkdirs();
}
try {
File newFilePath = new File(newPath +File.separator+ fileName);
newFilePath.createNewFile();
bw = new BufferedWriter(new OutputStreamWriter((new FileOutputStream(newPath +File.separator+ fileName)),"UTF-8"));
while((str = br.readLine()) != null){
System.out.println(str);
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*
*/
package com.neu.file;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;/**
* @author Administrator
*
*/
public class FileTest { public static void main(String[] args) {
String oldPath = "e:\\test\\test1.txt";
String newPath = "e:\\test2\\";
Copy(oldPath, newPath);
} private static void Copy(String oldPath, String newPath) {
BufferedReader br = null;
BufferedWriter bw = null;
String str = "";
String fileName = "";
File oldFile = new File(oldPath);
fileName = oldFile.getName();
if (oldFile.exists()) {
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(oldPath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("地址" + oldPath + "不存在文件");
e.printStackTrace();
}
} File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
try {
File newFilePath = new File(newPath + File.separator + fileName);
newFilePath.createNewFile();
bw = new BufferedWriter(
new OutputStreamWriter((new FileOutputStream(newPath
+ File.separator + fileName)), "UTF-8"));
while ((str = br.readLine()) != null) {
System.out.println(str);
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } finally {
//正是因为你没有关闭流所导致的问题
try {
if (null != bw)
bw.close();
if (null != br)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
public class FileTest {public static void main(String[] args) {
String oldPath = "f:\\test\\test1.txt";
String newPath = "f:\\test\\12.txt"; //加上你要创建的文件名
Copy(oldPath, newPath);
}private static void Copy(String oldPath, String newPath) {
BufferedReader br = null;
BufferedWriter bw = null;
String str = "";
String fileName = "";
File oldFile = new File(oldPath);
fileName = oldFile.getName();
if (oldFile.exists()) {
try {
//用BufferedReader作为读取文件内容的外管道
br = new BufferedReader(new FileReader(oldFile));
} catch (FileNotFoundException e) {
System.out.println("地址" + oldPath + "不存在文件");
e.printStackTrace();
}
}
File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
try {
File newFilePath = new File(newPath + File.separator + fileName);
newFilePath.createNewFile();
//用BufferedWriter作为向新文件写内容的外管道
bw = new BufferedWriter(new FileWriter(newFilePath));
while ((str = br.readLine()) != null) {
System.out.println(str);
bw.write(str);
bw.newLine();
}
br.close(); //用完后关闭流
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
路过是多个文件复制到一起,可以使用FileOutputStream(file,true)追加模式
out.flush();
out.close();
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;/**
* @author Administrator
*
*/
public class TestFile1 { public static void main(String[] args) {
String oldPath = "e:\\test\\test1.txt";
String newPath = "e:\\test2\\";
Copy(oldPath, newPath);
} private static void Copy(String oldPath, String newPath) {
BufferedReader br = null;
BufferedWriter bw = null;
String str = "";
String fileName = "";
File oldFile = new File(oldPath);
fileName = oldFile.getName();
if (oldFile.exists()) {
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(oldPath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("地址" + oldPath + "不存在文件");
e.printStackTrace();
}
} File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
try {
newFile = new File(newFile + File.separator + fileName);
bw = new BufferedWriter(
new OutputStreamWriter((new FileOutputStream(newFile)), "UTF-8"));
//LZ 你主要是在这里重复定义了文件路径,导致空指针错误 while ((str = br.readLine()) != null) {
System.out.println(str);
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } finally {
try {
if (null != bw)
bw.close();
if (null != br)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
LZ你看看我改的 对不对嘛
要养成好习惯~~~文本输出的话建议用 PrintWriter 呢
在构造的时候可以指定是否为追加模式, 是否立即输出缓冲池的内容
println()方法自动换行
蛮好用的
具体的 查帮助文档~~~~