黑马程序员技术交流社区
标题:
IO流必须掌握的知识点_一般人不给只给黑马兄弟
[打印本页]
作者:
zhoujiegun
时间:
2016-5-3 22:24
标题:
IO流必须掌握的知识点_一般人不给只给黑马兄弟
输入和输出Java代码 1.
//
Stream, Reader, Writer
2. Stream: 处 理字节流 3. Reader/Writer: 处理字符,通用Unicode 4. 5.
//
从标准输入设备读数据
6. 1. 用System.in的BufferedInputStream()读取字节 7.
int
b = System.in.read(); 8. System.out.println("Read data: " + (
char
)b);
//
强 制转换为字符
9. 2. BufferedReader 读取文本 10. 如果从Stream转成Reader,使用 InputStreamReader类 11. BufferedReader is =
new
BufferedReader(
new
12. InputStreamReader(System.in)); 13. String inputLine; 14.
while
((inputLine = is.readLine()) !=
null
) { 15. System.out.println(inputLine); 16.
int
val = Integer.parseInt(inputLine);
//
如果inputLine为整数
17. } 18. is.close(); 19. 20.
//
向标准输出设备写数据
21. 1. 用System.out的println()打印数据 22. 2. 用PrintWriter打印 23. PrintWriter pw =
new
PrintWriter(System.out); 24. pw.println("The answer is " + myAnswer + " at this time."); 25. 26.
//
Formatter类
27. 格 式化打印内容 28. Formatter fmtr =
new
Formatter(); 29. fmtr.format("%1$04d - the year of %2$f", 1951, Math.PI); 30. 或 者System.out.printf();或者System.out.format(); 31. 32.
//
原始扫描
33.
void
doFile(Reader is) { 34.
int
c; 35.
while
((c = is.read()) != -1) { 36. System.out.println((
char
)c); 37. } 38. } 39. 40.
//
Scanner扫描
41. Scanner 可以读取File, InputStream, String, Readable 42.
try
{ 43. Scanner scan =
new
Scanner(
new
File("a.txt")); 44.
while
(scan.hasNext()) { 45. String s = scan.next(); 46. } 47. }
catch
(FileNotFoundException e) { 48. e.printStackTrace(); 49. } 50. } 51. 52.
//
读取文件
53. BufferedReader is =
new
BufferedReader(
new
FileReader("myFile.txt")); 54. BufferedOutputStream bos =
new
BufferedOutputStream(
new
FileOutputStream("bytes.bat")); 55. is.close(); 56. bos.close(); 57. 58.
//
复制文件
59. BufferedIutputStream is =
new
BufferedIutputStream(
new
FileIutputStream("oldFile.txt")); 60. BufferedOutputStream os =
new
BufferedOutputStream(
new
FileOutputStream("newFile.txt")); 61.
int
b; 62.
while
((b = is.read()) != -1) { 63. os.write(b); 64. } 65. is.close(); 66. os.close(); 67. 68.
//
文件读入字符串
69. StringBuffer sb =
new
StringBuffer(); 70.
char
[] b =
new
char
[8192]; 71.
int
n; 72.
//
读一个块,如果有字符,加入缓冲区
73.
while
((n = is.read(b)) > 0) { 74. sb.append(b, 0, n); 75. } 76.
return
sb.toString(); 77. 78.
//
重定向标准流
79. String logfile = "error.log"; 80. System.setErr(
new
PrintStream(
new
FileOutputStream(logfile))); 81. 82.
//
读写不同字符集文本
83. BufferedReader chinese =
new
BufferedReader(
new
InputStreamReader(
new
FileInputStream("chinese.txt"), "ISO8859_1")); 84. PrintWriter standard =
new
PrintWriter(
new
OutputStreamWriter(
new
FileOutputStream("standard.txt"), "UTF-8")); 85. 86.
//
读取二进制数据
87. DataOutputStream os =
new
DataOutputStream(
new
FileOutputStream("a.txt")); 88. os.writeInt(i); 89. os.writeDouble(d); 90. os.close(); 91. 92.
//
从指定位置读数据
93. RandomAccessFile raf =
new
RandomAccessFile(fileName, "r");
//
r表示已 只读打开
94. raf.seek(15);
//
从15开始读
95. raf.readInt(); 96. raf.radLine(); 97. 98.
//
串行化对象
99. 对象串 行化,必须实现Serializable接口 100.
//
保存 数据到磁盘
101. ObjectOutputStream os =
new
ObjectOutputStream(
new
BufferedOutputStream(
new
FileOutputStream(FILENAME))); 102. os.writeObject(serialObject); 103. os.close(); 104.
//
读出数据
105. ObjectInputStream is =
new
ObjectInputStream(
new
FileInputStream(FILENAME)); 106. is.readObject(); 107. is.close();
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2