一个简单有趣的java文件输出

来源:互联网 发布:python数据挖掘 编辑:程序博客网 时间:2024/05/19 02:21
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码对照表:

十进制 十六进制 字符
  9 9 TAB(制表符)
10 A 换行
13 D 回车
32 20 空格
33 21 !
34 22 "
35 23 #
36 24 $
37 25 %
38 26 &
39 27 '
40 28 (
41 29 )
42 2A *
43 2B +
44 2C ,
45 2D -
46 2E .
47 2F /
48 30 0
49 31 1
50 32 2
51 33 3
52 34 4
53 35 5
54 36 6
55 37 7
56 38 8
57 39 9
58 3A :
59 3B ;
60 3C <
61 3D =
62 3E >
63 3F ?
64 40 @
65 41 A
66 42 B
67 43 C
68 44 D
69 45 E
70 46 F
71 47 G
72 48 H
73 49 I
74 4A J
75 4B K
76 4C L
77 4D M
78 4E N
79 4F O
80 50 P
81 51 Q
82 52 R
83 53 S
84 54 T
85 55 U
86 56 V
87 57 W
88 58 X
89 59 Y
90 5A Z


原创粉丝点击