(这部分比较抽象且写的不是很好,可能还要再编辑)

【概述】

流:流是一系列数据,包括输入流和输出流。你可以想象成黑客帝国的“代码雨”,只要我们输入指令,这些数据就像水一样流进流出了

IO:Input和OutPut,输入和输出文件

通过IO流,我们可以利用Java去读取来自文件的数据(目前阶段大多是记事本里面的数据)

下面列举了常见的流

因为我们只是初步了解使用IO流,并不需要全部了解这些流,下面会逐步写出现阶段用到的流

在使用之前,别忘了打上你的import java.io;

【BufferedReader】

BufferedReader类从字符输入流中读取文本并缓冲字符,以便有效地读取字符,数组和行

由Reader构成的每个读取请求都会导致相应的读取请求由基础字符或字节流构成,建议通过BufferedReader包装Reader的实例类以提高效率

可以暂时把BufferedReader理解为一个存储数据的,“缓冲流”

import java.io.*;
public class BRReadLines{
public static void main(String args[]) throws IOException{
BufferedReaderbr= new BufferedReader(new InputStreamReader(System.in));
String str;System.out.println("Enter lines of text.");
System.out.println("Enter 'end' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while (!str.equals("end"));
}
}

【FileInputStream】

选择一个现有文件作为输入流,这个路径可以在文件的“属性”里面复制,另外当文件处在原程序的文件夹里面,可以只写文件名不用写全部路径

InputStreamf = new FileInputStream("C:/java/hello");

或者

File f = new File("C:/java/hello");
InputStreamout = new FileInputStream(f);

FileInputStream中的一些方法

public void close() throws IOException{}

protected void finalize()throws IOException{}

public int read(int r)throws IOException{}

public int read(byte[] r) throws IOException{}

public int available() throws IOException{}

【FileOutputStream】

有Input就有Output

OutputStream f= new FileOutputStream("C:/java/hello");

或者

File f= new File("C:/java/hello");
OutputStream f= new FileOutputStream(f);

FileOutputStream中的一些方法

public void close() throws IOException{}

protected void finalize()throws IOException{}

public void write(int w)throws IOException{}

【一些实例代码】

A

public static void main(String args[]) throws IOException {
File f = new File("C:/Users/zhang/eclipse-workspace/HelloWord/src/lecture13/a1.txt");
// Make sure the path is correct!
// path coped from windows is C:\Users\zhang\eclipse-workspace\HelloWord\src\lecture13
FileOutputStream fop = new FileOutputStream(f);
// Create FileOutputStream object, a new file will be created if it does not exist. OutputStreamWriter writer = new OutputStreamWriter(fop, "gbk");
// Create OutputStreamWriter object, second argument is data format, gbk for windows, UTF-8 for Linux writer.append("Hello");
// Appends the specified character sequence to this writer. writer.append("\n");
// Appends a line return to this writer. writer.append("CS161FZ");
// Appends the specified character sequence to this writer. writer.close();
//Closes the stream, flushing it first. fop.close();
// Closes this file output stream and releases any system resources associated with this stream. FileInputStream fip = new FileInputStream(f);
// Create a FileInputStream对 object InputStreamReader reader = new InputStreamReader(fip, "gbk");
// Create a InputStreamReader object, same data format with the above StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
// convert to char, and add to StringBuffer object
}
System.out.println(sb.toString());
reader.close();
// close read stream fip.close();
// Closes this file input stream and releases any system resources associated with the stream.
}

B

	public static void main(String[] args) throws IOException {
File f = new File("C:/Users/zhang/eclipse-workspace/HelloWord/src/lecture13/test.txt");
FileOutputStream fop = new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fop, "gbk");
int datatoWrite[] = {11, 21, 3, 40, 5, 74, 89};
for (int i = 0; i < datatoWrite.length; i++) {
writer.append(Integer.toString(datatoWrite[i])); // writes the ints
writer.append("\n");
}
writer.close();
// If you forget to close the writer, YOU CAN NOT SUCESSFULLY WRITER!
fop.close(); FileInputStream fip = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fip, "gbk"));
while(br.ready()) {
System.out.println(br.readLine());
}
br.close();
fip.close();
}

最新文章

  1. 添加as源码
  2. 使用grid++report打印选中行
  3. Leetcode016 3Sum Closest
  4. jQuery怎样判断按钮是否被选中
  5. BZOJ_4892_[Tjoi2017]dna_哈希
  6. C#,单元测试
  7. 枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)
  8. 解决jar包乱码 in 创新实训 智能自然语言交流系统
  9. concurrent.futures模块
  10. vue-cli的工程如何正确使用Google Analytics?
  11. js获取iframe的id
  12. python爬虫脚本下载YouTube视频
  13. 20181009-7 选题 Scrum立会报告+燃尽图 06
  14. JTree 常用方法
  15. ssm框架搭建流程及原理分析
  16. IE的卸载之路(折腾1个多月,记录下。。)
  17. DataX-HDFS(读写)
  18. Luogu-4049 [JSOI2007]合金
  19. 怎样制作gif图片?怎样制作你项目的动态效果图到你的csdn?
  20. laravel 操作数据库

热门文章

  1. c++ string与wstring转换
  2. qt 获取窗口句柄的线程id和进程id GetWindowThreadProcessId
  3. 人物传记-BILL RAY:低谷时的信念,决定你能走多远
  4. BGV上线两天价格超过880美金,下一个YFI已到来!
  5. C++算法代码——骨牌铺法
  6. Java 优雅地退出程序
  7. tep环境变量、fixtures、用例三者之间的关系
  8. 研究了一下 Webpack 打包原理,顺手挣了个 AirPods Pro
  9. deepin-terminal改造之路
  10. MySQL注入与informantion_schema库