1.前言
学习Java也有一段时间了,在开发自己的安卓应用时总是觉得力不从心,主要是Java基础不好(自学的时候不够用心 ),所以还要回来补习Java。下面是我学习Java输入输出字节流的一些笔记,希望能帮到初学Java的朋友。
2.正文
- 确定使用输入字节流还是输出字节流。以程序为中心,往程序输入的就使用输入流,程序往外输出就使用输出流。
- 流分为字节流和字符流。字节流什么文件都可以处理,字符流主要处理纯文本文件。字节流的输入流抽象类是InputStream,实现类是FileInputStream;字节流的输出流抽象类是OutputStream,实现类是FileOutPutStream。字符流的输入流抽象类是Reader,实现类是FileReader;字节流的输出流抽象类是Writer,实现类是FileWriter。
- 使用流的一般过程。
1.与目标文件建立联系
2.选择对应的流
3.确定读取或者写入的块大小
4.读取或者写入
5.释放资源 - 下面是字节输入流读取文件的demo代码,注释很详细。
package demo03;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*读取文件Demo
*/
public class Demo01 {
public static void main(String[] args) {
//与目标文件建立联系
File file = new File("C:\\Users\\15321\\Desktop\\test.txt");
//选择流 InputStream是字节输入流的抽象类
InputStream is = null;
try {
//FileInputStream是字节输入流的实现类
is = new FileInputStream(file);
//确定读取块的大小 相当于缓冲
byte[] b = new byte[1024];
//实际读取的大小
int len;
//读取
while((len = is.read(b)) != -1) {
//输出 字节数组转成字符串
System.out.println(new String(b, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 下面是字节输出流的demo代码,想说的都写在注释了。
package demo03;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
1. 写入文件的demo
*/
public class Demo02 {
public static void main(String[] args) {
//与目标文件建立联系
File file = new File("C:\\Users\\15321\\Desktop\\test.txt");
//选择流
OutputStream os = null;
try {
String str = "woyebuzhidaoa";
//true表示追加文件 false表示覆盖文件
os = new FileOutputStream(file, true);
//写入的字节数据
byte[] data = str.getBytes();
//写入
os.write(data, 0, data.length);
//刷新
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 字节流的读取、写入综合练习。一个小的GUI文本类型查看器。demo代码如下。
package xyz.xinghai.demo03;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Demo {
public static void main(String[] args) {
//初始化窗口
JFrame frame = new JFrame("Demo");
frame.setSize(new Dimension(500, 500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化菜单
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItemOpen = new JMenuItem("Open");
JMenuItem menuItemSaveAs = new JMenuItem("Save as");
//初始化文本框
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("宋体", 20, 20));
//点代表当前路径
JFileChooser fileChooser = new JFileChooser(".");
menuItemOpen.addActionListener(e -> {
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
FileInputStream fis = null;
try {
textArea.setText("");
fis = new FileInputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) >= 0) {
textArea.append(new String(buffer, 0, len, "gbk"));
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
menuItemSaveAs.addActionListener(e -> {
int result = fileChooser.showSaveDialog(frame);
if(result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
FileOutputStream fos = null;
String s = textArea.getText();
try {
fos = new FileOutputStream(file);
byte[] data = s.getBytes();
fos.write(data, 0, data.length);
fos.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
//将菜单添加到窗口
menuBar.add(menu);
menu.add(menuItemOpen);
menu.addSeparator();
menu.add(menuItemSaveAs);
frame.setJMenuBar(menuBar);
//给文本框添加滚动条
JScrollPane scrollPane = new JScrollPane(textArea);
Container c = frame.getContentPane();
c.add(scrollPane);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
3.总结
- 如有错误恳请指正。
- 初学者一定要学好文件输入流和输出流,这样用起来才会轻松。
- 字符流的使用方法和字节流类似。
- 学好节点流之后再学缓冲流会很简单
- 与君共勉。