读写文件操作
对字符流文件读写
FileOutputStream 节点类 负责写字节
OutputStreamWriter 转化类 负责字节到字符转换
BufferedWriter 装饰类 负责写字符到缓存区
示例:
BufferedWriter b=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("路径")))
2 读文件
FileInputStream 节点类 负责写字节
InputStreamReader 转化类 负责字节到字符转换
BufferedReader装饰类 负责写字符到缓存区
示例:
BufferedReaderb=new BufferedReader(new InputStreamReader (new FileInputStream ("路径")))
进行对各种类型的二进制文件读写
FileOutputStream 节点类 负责写字节
BufferedOutputStream 装饰类 负责写字符到缓存区
DataOutputStreamWriter 转化类 负责数据类型到字节转换
示例:
DataOutputStreamWriter d=new DataOutputStreamWriter (new BufferedOutputStream(new FileOutputStream("路径")))
2 字节型读文件
FileInputStream 节点类 负责写字节
BufferedInputStream 装饰类 负责写字符到缓存区
DataInputStreamWriter 转化类 负责数据类型到字节转换
示例:
DataInputStreamWriter d=new DataInputStreamWriter (new BufferedInputStream(new FileInputStream ("路径")))
场景一:单机游戏的存档
存档:将游戏中的玩家对象从内存写入到硬盘上
----持久化的过程(需要对象的序列化支持)
读档:将保存在硬盘上的 对象数据读取到存档中转化成玩家对象,继续游戏
=========================================================
readme:
1.软件的基本功能:将用户输入以字节流 序列化 存到硬盘中,并读取
2.运行代码的方法:eclipse
3.简要的使用说明:输入用户等级与姓名
4.代码目录结构说明:
运用Serializable接口封装player类,主函数调用循环是否继续存取
package objstream;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.Scanner; /** * @author CF * @序列化与反序列化 * 前提:对象必须实现Serializable接口(序列化接口) * 2019年3月6日 * 下午8:37:47 */ public class objectstream { public static final String path = "E:\学习\java\程序\test\src\objstream\cc.txt"; public static void main(String[] args) { player player1 = new player(); Scanner input=new Scanner(System.in); System.out.println("请输入"); char a='y'; while(a=='y') { System.out.println("请输入姓名"); player1.setName(input.next()); System.out.println("请输入等级"); player1.setLevel(input.nextInt()); write(path,player1); reader(path); System.out.println("是否继续输入y/n"); a=input.next().charAt(0); } } public static void write(String path,player player1) { try( FileOutputStream fout=new FileOutputStream(path); ObjectOutputStream ostream=new ObjectOutputStream(fout) ){ ostream.writeObject(player1); System.out.println("!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void reader(String path) { try( FileInputStream finout=new FileInputStream(path); ObjectInputStream ostrea=new ObjectInputStream(finout); ){ player plater2=(player)ostrea.readObject(); System.out.println("读出成功:"+plater2.getName()+" "+plater2.getLevel()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } class player implements Serializable{ private static final long serialVersionUID = -1529270225938850227L; private String name; private int level; public player() {} public player(String n,int i) { setName(n); setLevel(i); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } }
====================================================================================================
readme:
1.对歌词进行适当改变,读取文件每行进行判断,再写入另一文件中
2.一函数字节流 一函数字符流 都可运行
package 歌词改编; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; /** * @author CF * @字符流转 * 2019年3月6日 * 下午7:01:24 */ public class 字符流 { public static final String path1 = "E:\学习\java\程序\test\src\歌词改编\music.txt"; public static final String path2 = "E:\学习\java\程序\test\src\歌词改编\change.txt"; public static final String path3 = "E:\学习\java\程序\test\src\歌词改编\change1.txt"; public static String bytstring(String path) { StringBuffer content=new StringBuffer(); Reader reader=null; BufferedReader breader=null; try { reader=new FileReader(path); breader=new BufferedReader(reader); String line=null; while((line=breader.readLine())!=null) { content.append(line); content.append(System.getProperty("line.separator"));//拼接一个系统默认分隔符 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { breader.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } return content.toString(); } public static boolean bytwrite(String str) { FileWriter write=null; BufferedWriter writ=null; try { write = new FileWriter(path2); writ=new BufferedWriter(write); writ.write(str); } catch (IOException e) { e.printStackTrace(); }finally { try { writ.close(); write.close(); } catch (IOException e) { e.printStackTrace(); } } return true; } public static void printwr(String str,String pathing,boolean iswrite){ try ( FileOutputStream output = new FileOutputStream(pathing); PrintWriter writer=new PrintWriter(output); ){ writer.append(str); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } public static void main(String[] args) { String str=bytstring(path1);//读文件 printwr(str, path3,true); // if(bytwrite(str)) // System.out.println("转录成功"); } }
===========================================================================================
readme:
对输入输出流进行演示
package 输入输出流; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * @author CF * @演示输入输出流用法 * 2019年2月25日 * 下午9:13:49 */ public class FileStreamDemo { private static final String path="C:/Users/59474/Desktop/java笔记/文件.txt"; private static final String path1="C:/Users/59474/Desktop/java笔记/lala.txt"; public static void main(String[] args) throws IOException { File file=new File(path); InputStream inputstream=new FileInputStream(file); byte[] bytes=new byte[20000]; inputstream.read(bytes); // for (int i = 0; i<bytes.length; i++) { // // bytes[i]=(byte)inputstream.read(); // } String content=new String(bytes); System.out.println(content); inputstream.close(); File file1=new File(path1); OutputStream out=new FileOutputStream(file1); String str="what`s your name?"; out.write(str.getBytes()); out.close(); } }
@@@@@@@@@@@@@@@@@@对文件函数的总结@@@@@@@@@@@@@@@@@@
content.append(System.getProperty("line.separator"));//拼接一个系统默认分隔符
字节流
InputStream
OutputStream //读/写byte型文件
子类
FileinputStream(file1)
FileOutputStream (file1) //打开文件
ObjectInputStream //对其目标文件进行强转成要得到的类型
ObjectOutputStream //序列化和反序列化(实现Serializable接口)
Reader
Writer //打开文件
子类
BufferedWriter //缓冲字符输出流用其write()方法写入字符串
BufferedReader
InputStreamReader
OutputStreamWriter
PrintWriter //用append拼接字符串
FileReader(path/file) //字符流读取文件
***************************以读取文件为例****************
两者都实例化后方可:
1.先用Reader/FileinputStream打开文件后
2.再用其他子类去对文件修改
=================================================================================
Readme:
1.给定地址进行网络文件的读取操作
package 网路文件读取; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; /** * @author CF * @从给定的URL上读取内容 * 2019年3月6日 * 下午7:50:04 */ public class readerURL { public static final String path2 = "E:\学习\java\程序\test\src\网路文件读取\read.html"; public static void main(String[] args) throws MalformedURLException { URL url=new URL("http://www.163.com/index.html"); //将inputreader转换为reader的套路:使用装饰器inputstreamreader StringBuffer str=new StringBuffer(); try(BufferedReader breader=new BufferedReader(new InputStreamReader(url.openStream()))) { String line=null; while((line=breader.readLine())!=null) { str.append(line); str.append(System.getProperty("line.separator")); } 歌词改编.字符流.printwr(str.toString(), path2, true); } catch (IOException e) { e.printStackTrace(); } } }