1、流的分类:
● 按流向:输入流、输出流;
● 按数据类型:字节流(8位字节)、字符流(16位字节)。

2、声明各个IO类
| File f=new File(fileName); |
| f.createNewFile(); |
| f.delete(); |
| f.mkdir(); |
| String[] str=f.list(); |
| File[] str=f.listFiles(); |
| # FileInputStream按数组读取,也有可能文件里没有这么多,做一个长度判断优化 |
| import java.io.*; |
| public class hello{ |
| public static void main(String[] args) throws IOException { |
| String fileName="D:"+File.separator+"hello.txt"; |
| File f=new File(fileName); |
| InputStream in=new FileInputStream(f); |
| byte[] b=new byte[1024]; |
| int len=in.read(b); |
| in.close(); |
| System.out.println("读入长度为:"+len); |
| System.out.println(new String(b,0,len)); |
| } |
| } |
| # 按照基础数据类型读取 |
| public class DataOutputStreamDemo{ |
| public static void main(String[] args) throws IOException{ |
| File file = new File("d:" + File.separator +"hello.txt"); |
| DataInputStream input = new DataInputStream(new FileInputStream(file)); |
| char[] ch = new char[10]; |
| int count = 0; |
| char temp; |
| while((temp = input.readChar()) != 'C'){ |
| ch[count++] = temp; |
| } |
| System.out.println(ch); |
| } |
| } |
| # FileOutputStream覆盖和追加 |
| import java.io.*; |
| class hello{ |
| public static void main(String[] args) throws IOException { |
| String fileName="D:"+File.separator+"hello.txt"; |
| File f=new File(fileName); |
| OutputStream out =new FileOutputStream(f); |
| String str="Hello World"; |
| byte[] b=str.getBytes(); |
| out.write(b); |
| out.close(); |
| } |
| } |
| |
| class hello{ |
| public static void main(String[] args) throws IOException { |
| String fileName="D:"+File.separator+"hello.txt"; |
| File f=new File(fileName); |
| OutputStream out =new FileOutputStream(f,true); |
| String str="Rollen"; |
| |
| byte[] b=str.getBytes(); |
| for (int i = 0; i < b.length; i++) { |
| out.write(b[i]); |
| } |
| out.close(); |
| } |
| } |
| |
| # DataOutputStream类示例 |
| import java.io.DataOutputStream; |
| import java.io.File; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| public class DataOutputStreamDemo{ |
| public static void main(String[] args) throws IOException{ |
| File file = new File("d:" + File.separator +"hello.txt"); |
| char[] ch = { 'A', 'B', 'C' }; |
| DataOutputStream out = null; |
| out = new DataOutputStream(new FileOutputStream(file)); |
| for(char temp : ch){ |
| out.writeChar(temp); |
| } |
| out.close(); |
| } |
| } |
| # FileReader循环读取 |
| import java.io.*; |
| class hello{ |
| public static void main(String[] args) throws IOException { |
| String fileName="D:"+File.separator+"hello.txt"; |
| File f=new File(fileName); |
| char[] ch=new char[100]; |
| Reader read=new FileReader(f); |
| int temp=0; |
| int count=0; |
| while((temp=read.read())!=(-1)){ |
| ch[count++]=(char)temp; |
| } |
| read.close(); |
| System.out.println("内容为"+new String(ch,0,count)); |
| } |
| } |
| # BufferedReader读取输入,需要借助父类InputStreamReader将字节流转换成字符流 |
| public class BufferedReaderDemo{ |
| public static void main(String[] args){ |
| BufferedReader buf = new BufferedReader( |
| newInputStreamReader(System.in)); |
| String str = null; |
| System.out.println("请输入内容"); |
| try{ |
| str = buf.readLine(); |
| }catch(IOException e){ |
| e.printStackTrace(); |
| } |
| System.out.println("你输入的内容是:" + str); |
| } |
| } |
| import java.io.*; |
| class hello{ |
| public static void main(String[] args) throws IOException { |
| String fileName="D:"+File.separator+"hello.txt"; |
| File f=new File(fileName); |
| Writer out =new FileWriter(f); |
| String str="hello"; |
| out.write(str); |
| out.close(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通