Java IO
InputStream&OutputStream
public class Copy {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("para missed");
System.exit(1);
}
File f1 = new File(args[0]);
File f2 = new File(args[1]);
InputStream input = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
if (input != null && out != null) {
int temp = 0;
while ((temp = input.read()) != -1) {
out.write(temp);
}
System.out.println("finish");
input.close();
out.close();
}
}
}
Reader
public class RederTest {
private static final String fileName = "D:\\test.txt";
public static void main(String[] args) {
BufferedReader br = null;
StringBuffer sb = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GBK"));
sb = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("文件 " + fileName + "的内容:==>> " + sb);
}
}
Writer
public class WriterTest {
public static void main(String[] args) throws IOException {
File f = new File("D:" + File.separator + "test.txt");
Writer out = new FileWriter(f, true);
System.out.println("输入内容,按回车键结束");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
out.write(str);
out.flush();
out.close();
}
}