字节输入流,按照字节的方式进行数据读取
public static void main(String[] args) throws IOException {
FileInputStream in=null;
try {
in = new FileInputStream("D:/a.txt");
int data = 0;
//read 读取一个字节,光标向下,并返回对应的ASCLL码值,返回为int型,到末尾就返回-1
while ((data = in.read()) != -1) {
System.out.println((char) data);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
try (FileInputStream f = new FileInputStream("D:/a.txt")) {
// available 表示可以读取的字节数,返回int
System.out.println(f.available());
byte[] b = new byte[f.available()];
int data = 0;
//重载后的read返回的是数组中元素的数量,而不是元素
while ((data = f.read(b)) != -1) {
// 获取数组中的元素转化为字符。数组 , 起始位置(包含) , 个数
System.err.println(new String(b, 0, data));
}
} catch (Exception e) {
// TODO: handle exception
}
字节流输出: 数组是字节数组 也就是 byte
public class zijie_out {
public static void main(String[] args) {
byte[] bytes = { 64, 66, 54, 7, 93 };
// 自动关闭资源写法
try (FileOutputStream f = new FileOutputStream("D:/a.txt");) {
// 写出对应的Ascll码
f.write(99);
// 刷新缓存,强制持久化
f.flush();
// 写出数组
f.write(bytes);
// 不能直接字符串
String string = "你适合低";
byte[] by = string.getBytes();
f.write(by);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
对覆盖的时候写入时: 加true ,不会,覆盖
public class zijie_out_02 {
public static void main(String[] args) {
// 覆盖写入,当创建该文件的输出流对象的时候,就会把该文件中内容全部清除
// FileOutputStream fos1 = new FileOutputStream("D:/a.txt");
// 创建对象时,传入第二个参数true说明是追加写入,则不会清空该文件
FileOutputStream fos1 = null;
try {
fos1 = new FileOutputStream("D:/a.txt", true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos1.write(97);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字符流:读文本
FileReader:一次读取两个字节,解决乱码问题
read():一次读取一个字符返回对应的ascll码,打到末尾返回-1
read(char[]) : -次读取一个字符数组,提高读取效率,返回本地读取的字符个数,到达文件末尾返回-1
public static void main(String[] args) throws IOException {
// read():一次读取一个字符返回对应的ascll码,打到末尾返回-1
try (
// 读入字符
FileReader f = new FileReader("D:/a.txt");)
// 遍历输出
{
int temp = 0;
while ((temp = f.read()) != -1) {
System.out.println((char) temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// read(char[]) : -次读取一一个字符数组,提高读取效率,返回本地读取的字符个数,到达文件末尾返回-1
try (FileReader fe = new FileReader("D:/a.txt")) {
int data = 0;
char[] c = new char[10];
while ((data = fe.read(c)) != -1) {
// data返回1的是数组中的元素的个数
// String(a,b,c):a表示要输出的数组,b表示输出的开始位置,c表示输出个数
System.out.println(new String(c, 0, data));
}
} catch (Exception e) {
// TODO: handle exception
}
}
字符输出:能直接操作字符串
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("D:/a.txt");) {
// 数组
char[] c = { 'a', 'g', 'f' };
fw.write(c);
// 直接写字符
fw.write("集合不答复哈");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
字节字符输入缓冲:
public class huanChong_in {
public static void main(String[] args) {
}
// 字符缓冲
public static void zifu() {
try (// 创建字符
FileReader fr = new FileReader("D:/a.txt");
// 创建缓冲
BufferedReader bf = new BufferedReader(fr);) {
String data = null;
// br.readLine() : 读取一行数据,返回读取到的这一行数据的内容,为String , 到达文件末尾返回 null
while ((data = bf.readLine()) != null) {
System.out.print(data);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void zijie() {
// 好像字节类的不能直接操作字符串类型
try (// 字节流
FileInputStream fe = new FileInputStream("D:/a.txt");
// 字节输入缓冲
BufferedInputStream bu = new BufferedInputStream(fe);) {
// 没有readline 操作
int data = 0;
while ((data = bu.read()) != -1) {
System.out.print((char) data);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
字节字符输出缓冲:
public class huanChong_out {
public static void main(String[] args) {
try (// 字节流
FileOutputStream fe = new FileOutputStream("D:/a.txt");
// 字节缓冲
BufferedOutputStream bf = new BufferedOutputStream(fe);) {
byte[] b = { 5, 6, 7, 2, 4 };
// 果然字节直接操作的是字节型,和int
bf.write(654);
bf.write(b);
// 刷缓存
bf.flush();
} catch (Exception e) {
// TODO: handle exception
}
}
public static void zifu() {
try (// 字符流
FileWriter fe = new FileWriter("D:/a.txt");
// 字符缓冲
BufferedWriter bw = new BufferedWriter(fe);) {
// 直接写字符
bw.write("直接写字符串");
// 换行
bw.newLine();
bw.flush();
} catch (Exception e) {
// TODO: handle exception
}
}
}
转换流: 字节向字符转化,防止有乱码
public class zhaunHuan {
public static void main(String[] args) throws IOException {
try (// 得到某个文件
FileInputStream f = getFileInputStream("D:/a.txt");) {
// 字节转字符
InputStreamReader in = new InputStreamReader(f);
int data = 0;
while ((data = in.read()) != -1) {
System.err.println((char) data);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 需求:根据文件路径,获取文件的输入流对象
public static FileInputStream getFileInputStream(String filepath)
throws FileNotFoundException {
return new FileInputStream(filepath);
}
}
打印流:
public static void main(String[] args) throws FileNotFoundException {
// 创建输出流
FileOutputStream outputStream = new FileOutputStream("D:/a.txt");
// 封装打印流
PrintStream po = new PrintStream(outputStream);
// 字符字节都行
po.print(65863);
po.print("kdhfday");
// 更改System中的打印流
System.setOut(po);
// 以下所有打印操作,不再显示在控制台中,而是打印到指定文件中
System.out.println("xxxxxxxxx");
System.out.println("你好吗");
}
数据流是为了不同平台之间数据读取的统一-性,Linux,Windows等操作系统对数据进行存储的时候方式是不同的 为了解决之间的差异性java提供了两个轻量级的方法,只要每个平台有java环境就能保证数据的一致性 比如在进行网络协议传输的时候,是非常适用的
public class shuJu_in {
public static void main(String[] args) throws Exception {
// 创建对象
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
"D:/a.txt"));
// 写出数据
dos.writeByte(1);
dos.writeShort(12);
dos.writeInt(51);
dos.writeUTF("阿萨德吉安市");
dos.flush();
dos.close();
}
}
public class shuJu_out {
public static void main(String[] args) throws Exception {
DataInputStream dis = new DataInputStream(new FileInputStream(
"D:/a.txt"));
// 必须保证 顺序和类型一致
System.out.println(dis.readByte());
System.out.println(dis.readShort());
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
}
}