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

【概述】

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

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();
}

【Java】流、IO(初步)的更多相关文章

  1. Java的IO流以及输入流与输出流的异同

    一:流的基本概念:           Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.J ...

  2. java的IO流

    java的IO流继承四大抽象类分别是字节流 inputStream outputStream与字符流 read write.怎么理解记忆很重要. 直接连接读写对象的是结点流,例如对文件读取字节类的名字 ...

  3. Java基础——IO流

    今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...

  4. 【Java】IO流简单分辨

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用 ...

  5. Java - 文件(IO流)

    Java - 文件 (IO)   流的分类:     > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter     ...

  6. Java中IO流的总结

    有关Java中IO流总结图 流分类 按方向分 输入流 输出流 按单位分 字节流 字符流 按功能分 节点流 处理流(过滤流) 其他 所有的流继承与这四类流:InputSteam.OutputStream ...

  7. Java之IO流详解

    IO流 Input/Output 完成输入/输出 应用程序运行时——数据在内存中  ←→ 把数据写入硬盘(磁带)  内存中的数据不可持久保存的  输入:从外部存储器(硬盘.磁带.U盘)把数据读入内存. ...

  8. Java笔记:Java 流(Stream)、文件(File)和IO

    更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...

  9. JAVA基础-IO流(一)

    一.IO流 IO流是Java为方便我们对文件的读写进行操作而提供的一种技术.按照读取写入文件的方式不同可以分为字符流和字节流,而每个流派按照功能又分为读和写.字符流读写操作的根类为Reader和Wri ...

  10. JAVA中IO流总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...

随机推荐

  1. NGK公链脱颖而出,成为值得期待的项目!

    当下2020年是动荡的一年,全世界经济危机汲汲可危,在这个特殊的时刻,有人抱怨说这是最坏的年代,也有人庆幸说这是最好的年代,历史不会重演,但总是惊人的相似,首先带你回顾一下上一次金融危机出现的2008 ...

  2. NGK:APP一站式挖矿高收益项目

    NGK是10月中旬刚上线的公链项目,采用手机挖矿形式.NGK数字增益平台,200美金即可入场,收益可观,分为静态和动态两种,投资算力收益超高.邀请好友挖矿还有额外的返佣. NGK立志为所有人创造无差别 ...

  3. “NGK公链+5G”——打造智慧城市

    智慧城市目前被全球各国当成城市建设的重点,旨在城市在智能化的同时,还能给民众带来幸福感和安全感.随着5G的到来,城市智能化又到了一个新的高度.比如无人驾驶.无人机等方面将会产生质的变化,因为5G的加入 ...

  4. 17_MySQL分组查询的应用

    本节涉及SQL语句: -- 分组查询 SELECT deptno,AVG(sal) FROM t_emp GROUP BY deptno; -- 四舍五入 SELECT deptno,ROUND(AV ...

  5. Redis高频面试题总结

    通过面试多家大型互联网企业,总结了如下的高频面试题目: 1.redis 过期键的删除策略? (1)定时删除:在设置键的过期时间的同时,创建一个定时器 timer). 让定时器在键的过期时间来临时,立即 ...

  6. net字符串倒置和冒泡排序

    using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...

  7. 答不上的JUC笔试题

    1:有一个总任务A,分解为子任务A1 A2 A3 ...,任何一个子任务失败后要快速取消所有任务,请写程序模拟. 「请寻求最优解,不要只是粗暴wait()」 本题解题思路:Fork/Join 通常使用 ...

  8. MySQL注入与informantion_schema库

    目录 只可读 自动开启 和MySQL注入有关的3个表 手动注入的使用案例 表介绍 查询一个表中全部字段的过程 MySQL V5.0安装完成会默认会生成一个库(informantion_schema), ...

  9. 《C++ Primer》笔记 第2章 变量和基本类型

    如果你的数值超过了int表示范围,选用long long 如果你需要使用一个不大的整数,那么明确指定它的类型是signed char或者unsigned char 执行浮点数运算选用double 当一 ...

  10. HDOJ-1213(简单并查集)

    How many tables HDOJ-1213 #include<iostream> #include<cstring> #include<cstdio> #i ...