package bao5;
/**
* 第3章File I/O
* 3.使用BufferedReader和BufferedWriter复制C:/source.txt到D:target.txt。
* 查看文件内容和文件大小是否一致。
*/
import java.io.*;
* 第3章File I/O
* 3.使用BufferedReader和BufferedWriter复制C:/source.txt到D:target.txt。
* 查看文件内容和文件大小是否一致。
*/
import java.io.*;
public class Buffered {
public static void main(String[] args) {
File file1 = new File("C:/source.txt");
File file2 = new File("C:/target.txt");
public static void main(String[] args) {
File file1 = new File("C:/source.txt");
File file2 = new File("C:/target.txt");
BufferedReader reader = null;
BufferedWriter writer = null;
BufferedWriter writer = null;
FileReader fileReader = null;
FileWriter fileWriter = null;
FileWriter fileWriter = null;
StringBuffer stf = null;
try {
// 创建FileReader对象
fileReader = new FileReader(file1);
reader = new BufferedReader(fileReader);
try {
// 创建FileReader对象
fileReader = new FileReader(file1);
reader = new BufferedReader(fileReader);
// 创建FileWriter对象
fileWriter = new FileWriter(file2);
writer = new BufferedWriter(fileWriter);
fileWriter = new FileWriter(file2);
writer = new BufferedWriter(fileWriter);
stf = new StringBuffer(); // 创建StringBuffer对象
String str;
while ((str = reader.readLine()) != null) {
stf.append(str);
stf.append("\n");
while ((str = reader.readLine()) != null) {
stf.append(str);
stf.append("\n");
writer.write(str);
writer.newLine();
}
System.out.println("源文件的大小为:" + file1.length() + "字节");
System.out.println("源文件的内容为:" + "\n" + stf.toString());
writer.flush();
writer.newLine();
}
System.out.println("源文件的大小为:" + file1.length() + "字节");
System.out.println("源文件的内容为:" + "\n" + stf.toString());
writer.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.close();
reader.close();
fileWriter.close();
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
reader.close();
fileWriter.close();
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
BufferedReader reader2 = null;
FileReader fileReader2 = null;
StringBuffer buf = new StringBuffer();
try {
fileReader2 = new FileReader(file2);
reader2 = new BufferedReader(fileReader2);
FileReader fileReader2 = null;
StringBuffer buf = new StringBuffer();
try {
fileReader2 = new FileReader(file2);
reader2 = new BufferedReader(fileReader2);
String str2;
while ((str2 = reader2.readLine()) != null) {
buf.append(str2);
buf.append("\n");
}
System.out.println("复制后的文件的大小为:" + file2.length() + "字节");
System.out.println("复制后的文件的内容为:" + "\n" + buf.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
reader2.close();
fileReader2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
while ((str2 = reader2.readLine()) != null) {
buf.append(str2);
buf.append("\n");
}
System.out.println("复制后的文件的大小为:" + file2.length() + "字节");
System.out.println("复制后的文件的内容为:" + "\n" + buf.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
reader2.close();
fileReader2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package bao5;
/**
* 第3章FileI/O 4.编写一个Java程序读取Windows目录下的win.ini文件,并输出其内容。
*
*/
import java.io.*;
* 第3章FileI/O 4.编写一个Java程序读取Windows目录下的win.ini文件,并输出其内容。
*
*/
import java.io.*;
public class Windows {
public static void main(String[] args) {
public static void main(String[] args) {
File file = new File("C:/Windows/win.ini");
FileInputStream inputStream1 = null;
DataInputStream inputStream2 = null;
FileInputStream inputStream1 = null;
DataInputStream inputStream2 = null;
try {
inputStream1 = new FileInputStream(file);
inputStream2 = new DataInputStream(inputStream1);
int temp = 0;
while ((temp = inputStream2.read()) != -1) {
System.out.print(temp + " ");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
inputStream2.close();
inputStream1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inputStream1 = new FileInputStream(file);
inputStream2 = new DataInputStream(inputStream1);
int temp = 0;
while ((temp = inputStream2.read()) != -1) {
System.out.print(temp + " ");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
inputStream2.close();
inputStream1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}