public class TestIO {
public static void main(String[] args) {
File inputFile = new File("a.txt");//这个地方要考虑到a.txt和b.txt在项目中的位置
File outputFile = new File("b.txt");
//b.txt文件不存在,可以创建
if(outputFile.exists()){
}else{
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream inputStream = null;
OutputStream outputStream = null;
int temp;
try {
inputStream = new FileInputStream(inputFile);
outputStream = new FileOutputStream(outputFile);
//读取a.txt数据并写到b.txt文件中
while((temp = inputStream.read()) != -1){
outputStream.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Runtime.getRuntime().exit(-1);
} catch(IOException e){
e.printStackTrace();
} finally{
try{
//一定要关闭,否则可能得不到结果
inputStream.close();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}