import java.io.*;
class Test {
public static void main(String[] args) {
try {
FileInputStream in
= new FileInputStream("foo.txt");
FileOutputStream out
= new FileOutputStream("bar.txt");
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
}
}
--------------------------------------
很简单的java文件输出
中间的int c 是什么呢?
打出来System.out.println(c);
foo.txt的内容
aa
bb
cc
打印的结果
97
97
13
10
98
98
13
10
99
99
13
10
这些数字是什么呢?
这些数字是10进制ASCII码
------另外一个例子------------
public class ReadWriteFile {
public static void main(String[] args) {
try {
File read = new File("c:\\1.txt");
File write = new File("c:\\2.txt");
BufferedReader br = new BufferedReader(new FileReader(read));
BufferedWriter bw = new BufferedWriter(new FileWriter(write));
String temp = null;
temp = br.readLine();
while (temp != null) {
// 写文件
bw.write(temp + "\r\n"); // 只适用Windows系统
// 继续读文件
temp = br.readLine();
}
bw.close();
br.close();
} catch (FileNotFoundException e) { // 文件未找到
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
}
-----------附录---------
以下是常用的ASCII码对照表:
十进制十六进制 字符 99TAB(制表符) 10A换行 13D 回车 3220空格 3321! 3422" 3523# 3624$ 3725% 3826& 3927' 4028( 4129) 422A* 432B+ 442C, 452D- 462E. 472F/ 48300 49311 50322 51333 52344 53355 54366 55377 56388 57399 583A: 593B; 603C< 613D= 623E> 633F? 6440@ 6541A 6642B 6743C 6844D 6945E 7046F 7147G 7248H 7349I 744AJ 754BK 764CL 774DM 784EN 794FO 8050P 8151Q 8252R 8353S 8454T 8555U 8656V 8757W 8858X 8959Y 905AZ