Java IO文件流
来源:互联网 发布:win10美化mac 编辑:程序博客网 时间:2024/06/06 04:09
创建文件
//add file File file =new File("E:\\java用文件","file.txt"); try{ file.createNewFile(); System.out.println("creat ok!"); } catch (IOException e){ System.out.println("creat go die!"); e.printStackTrace(); }
删除文件
//deletefile.delete();
读取文件数据
//way firstFile file =new File("E:\\java用文件","file.txt"); InputStream input = null; byte b[] = new byte[(int)file.lenght()]; //根据文件的大小定义数组空间 try { input = new FileInputStream(file); input.read(b); input.close(); }catch (IOException ee){ ee.printStackTrace(); }//way second 常用File file =new File("E:\\java用文件","file.txt"); InputStream input = null; byte b[] = new byte[(int)file.length()]; int temp = 0; int len = 0; try{ input = new FileInputStream(file); //使用FileInputStream为InputStream实例化 while((temp = input.read())!= -1) { //使用while循环读取文件内容,到达结尾是temp=-1 b[len] = (byte)temp; len++; } input.close(); //关闭输入流 }catch(IOException ex){ //捕捉异常 ex.printStackTrace(); } System.out.println(new String(b));
向文件写入内容
//会覆盖文件原本内容File file =new File("E:\\java用文件","file.txt"); OutputStream out = null; //声明 OutputStream对象 String str = "Hello"; byte b[] = str.getBytes(); //把字符串转换为b数组 try{ out = new FileOutputStream(file); //用 FileOutputStream实例化OutputStream out.write(b); //写入数据 out.close(); //关闭输出流 System.out.println("write ok!"); }catch (IOException ev){ //捕捉异常 System.out.print("write go die!"); ev.printStackTrace(); //打印异常 }//在末尾追加内容//将传入FileOutputStream 构造方法中的参数append赋值为true即可//public FileOutputStream(File dile,boolean append)throws//FileNotFoundException File file =new File("E:\\java用文件","file.txt"); OutputStream out = null; //声明 OutputStream对象 String str = "Hello"; byte b[] = str.getBytes(); //把字符串转换为b数组 try{ out = new FileOutputStream(file,true); //用 FileOutputStream实例化OutputStream,添加true!!! out.write(b); //写入数据 out.close(); //关闭输出流 System.out.println("write ok!"); }catch (IOException ev){ //捕捉异常 System.out.print("write go die!"); ev.printStackTrace(); //打印异常 }//若想换行,可以在需要换行的字符串处添加转义字符"\r\n"即可
字符流
读取文件内容
File file =new File("E:\\java用文件","file.txt"); Reader read = null; //声明Reader对象 char c[] = new char[(int)file.length()]; try { read = new FileReader(file); //使用FileReader实例化Reader read.read(c); read.close(); }catch (IOException ee){ System.out.println("read go die !"); ee.printStackTrace(); } System.out.println(c);
使用while方法读取
File file =new File("E:\\java用文件","file.txt"); Reader read = null; //声明Reader对象 char c[] = new char[(int)file.length()]; int temp = 0; int len = 0; try { read = new FileReader(file); //使用FileReader实例化Reader while((temp = read.read())!=-1) { //用while读取 到达结尾时temp = -1 c[len] = (char)temp; len++; } read.close(); }catch (IOException ee){ System.out.println("read go die !"); ee.printStackTrace(); } System.out.println(c);
写入
//way1File file =new File("E:\\java用文件","file.txt"); Writer out = null; String str = "lllllll\r\n"; char c[] = str.toCharArray(); try{ out = new FileWriter(file,true); out.write(c); out.close(); System.out.println("write ok!"); }catch (IOException ee) { System.out.print("write go die!"); ee.printStackTrace(); }//way2File file =new File("E:\\java用文件","file.txt"); Writer out = null; String str = "lllllll\r\n"; try{ out = new FileWriter(file,true); out.write(str); out.close(); System.out.println("write ok!"); }catch (IOException ee) { System.out.print("write go die!"); ee.printStackTrace(); }
字节流
字节输入流
File file = new File ("E:\\java用文件","file.txt"); InputStream input = null; //声明InputStream对象 byte b[]=new byte[(int)file.length()]; int temp = 0; int len = 0; try{ input = new FileInputStream(file); //使用FileInputStream为 InputStream实例化 while((temp=input.read())!=-1) { b[len]=(byte)temp; len++; } input.close(); }catch(IOException eq){ eq.printStackTrace(); } System.out.println(new String(b));
字节输出流
File file = new File ("E:\\java用文件","file.txt"); OutputStream out = null; String str = "el psy congroo"; byte b[] = str.getBytes(); try{ out = new FileOutputStream(file,true); out.write(b); out.close(); System.out.println("write ok!!!"); }catch(IOException ew){ ew.printStackTrace(); System.out.println("write go die"); }
对象序列化
对象序列化是将对象状态转换为可保持或传输的格式的过程
若需要某个对象能支持序列化机制,必须实现Serializable接口
class student implements Serializable{ String number; String name; String sex; String birthday; String department; public student(String number,String name,String sex,String birthday,String department){ this.number = number; this.name = name; this.sex = sex; this.birthday = birthday; this.department = department; }}
对象输出流
File file =new File("E:\\java用文件","file.txt"); String name = jtname.getText(); String number = jtnumber.getText(); String sex = jtsex.getText(); String birthday = jtbirthday.getText(); String department = jtdepartment.getText(); student stu = new student(number,name,sex,birthday,department); FileOutputStream fout = null; ObjectOutputStream oos = null; try{ fout = new FileOutputStream(file,true); //实例化文件输出流 oos = new ObjectOutputStream(fout); //实例化对象输出流 oos.writeObject(stu); //把对象写入输出流 oos.close(); //关闭对象输出流 }catch (IOException er){ er.printStackTrace(); System.out.println("writeobject go die"); }
对象输入流
File file =new File("E:\\java用文件","file.txt"); FileInputStream fis = null; ObjectInputStream ois = null; try{ fis = new FileInputStream(file); ois = new ObjectInputStream(fis); student stu = (student)ois.readObject(); //读取stu对象需要强制转换 ois.close(); jname.setText(stu.name); jsex.setText(stu.sex); jbirthday.setText(stu.birthday); jdepartment.setText(stu.department); }catch (IOException ex){ //捕捉IOException异常 ex.printStackTrace(); }catch(ClassNotFoundException we){ //捕捉ClassNotFoundException异常 we.printStackTrace(); }
0 0
- Java IO文件流
- JAVA IO-文件流
- java 文件IO流
- Java - 文件(IO流)
- java io流读写文件
- java学习---文件IO流
- 《java----IO流---文件复制》
- Java IO流读取文件
- Java IO流读取文件
- java文件IO流复习
- Java IO流读取文件
- Java IO 流读取文件
- Java IO流文件操作
- java IO流读写文件
- Java、Android文件IO流
- Java IO流读取文件
- java IO流遍历文件
- Java IO流读取文件
- openwrt支持pl2303usb转串口设备
- 笔记本安装虚拟机
- 线性回归正则化 regularized linear regression
- angularjs How to set focus on input field?
- mysql反转pojo实体类 (注解)
- Java IO文件流
- 构建之法 2
- [HDU 5029] Relief grain (树链剖分+线段树)
- 思想动态
- Failed to compile cuda_ndarray.cu: libcublas.so.7.5: cannot open shared object file
- Angularjs Controller 间通信机制
- 项目布署到linux
- Eclipse中使用git管理代码
- 树形dp-CodeForces 581F