栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java读写文件方法总结(推荐)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java读写文件方法总结(推荐)

Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便。奈何我的记性实在是叫人着急,很多时候既然都会想不起来怎么写了,不过我的Java代码量也实在是少的可怜,所以应该多多练习。这里做一个总结,集中在一起方面今后查看。

Java读文件

package 天才白痴梦;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;

public class JavaIO {
  
  
  
  
  public static void readFileByBytes(String filename) {
    File file=new File(filename);
    FileInputStream in=null;
    try {
      System.out.println("以字节为单位读取文件,一次读一个字节: ");
      in=new FileInputStream(file);
      int temp=0;
      while ((temp=in.read()) != -1) {
 System.out.println(temp);
      }
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
      return ;
    }
    try {
      System.out.println("以字节为单位读取文件,一次读多个字节: ");
      byte[] temp=new byte[100];
      int byteread=0;
      in=new FileInputStream(file);
      JavaIO.showAvailableBytes(in);
      while ((byteread=in.read(temp)) != -1) {
 System.out.write(temp,0,byteread);
      }
    } catch (Exception e1) {
      e1.printStackTrace();
    } finally {
      if (in != null) {
 try {
   in.close();
 } catch (IOException e1) {
   
 }
      }
    }
  }
  
  public static void readFileByChar(String filename) {
    File file=new File(filename);
    Reader reader=null;
    try {
      System.out.println("以字符为单位读取文件内容,一次一个字节:");
      //InputStreamReader类:是字节向字符转换的桥梁
      reader=new InputStreamReader(new FileInputStream(file));
      int temp;
      while ((temp=reader.read()) != -1) {
 if (((char)temp) != 'r') {
   System.out.println((char)temp);
 }
      } 
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      System.out.println("以字符为单位读取文件内容,一次读多个字节: ");
      char[] temp=new char[30];
      int charread=0;
      reader=new InputStreamReader(new FileInputStream(filename));
      while ((charread=reader.read(temp)) != -1) {
 if ((charread == temp.length) && (temp[temp.length-1]!='r')) {
   System.out.println(temp);
 } else {
   for (int i=0; i 4 ? 4 : 0);
      randomfile.seek(beginIndex);
      byte[] bytes=new byte[10];
      int byteread=0;
      while ((byteread=randomfile.read(bytes)) != -1) {
 System.out.write(bytes,0,byteread);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (randomfile != null) {
 try {
   randomfile.close();
 } catch (IOException e) {
   
 }
      }
    }
  }
  private static void showAvailableBytes(InputStream in) {
    try {
      System.out.println("当前字节输入流中的字节数为:" + in.available());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
    String filename="E:\BaiYiShaoNian.txt";
    JavaIO.readFileByBytes(filename);
    JavaIO.readFileByChar(filename);
    JavaIO.readFileByLine(filename);
    JavaIO.readFileByRandomAccess(filename);
  }
}

Java写文件

package 天才白痴梦;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class JavaIO2 {

  public static void main(String[] args) throws IOException {
    String Path="E:\天才白痴梦\JAVA";
    File file=new File("E:\天才白痴梦\JAVA","BaiYiShaoNian.txt");
    if (!file.exists()) {
      try {
 file.createNewFile();
      } catch (IOException e) {
 e.printStackTrace();
      }
    }
    
    FileOutputStream fos=null;
    BufferedWriter bw=null;
    FileWriter fw=null;
    int value=1000;
    
    try {
      fos=new FileOutputStream(new File(Path+"fos.txt"));
      long begin=System.currentTimeMillis();
      for (int i=1; i<=value; i++) {
 fos.write(5);
      }
      long end=System.currentTimeMillis();
      System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
      fos.close();
      
      bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
      begin=System.currentTimeMillis();
      for (int i=1; i<=value; i++) {
 bw.write(5);
 bw.newline();
      }
      bw.close();
      end=System.currentTimeMillis();
      System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));
      
      fw=new FileWriter(Path+"fw.txt");
      begin=System.currentTimeMillis();
      for (int i=1; i<=value; i++) {
 fw.write(5); 
      }
      fw.close();
      end=System.currentTimeMillis();
      System.out.println("TheCostTime of FileWriter is : " + (end-begin));
      
      
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
 fos.close(); //FileOutputStream
 bw.close(); //BufferedWriter
 fw.close(); //FileWriter
      } catch (Exception e) {
 e.printStackTrace();
      }
    }
    
  }
}

以上这篇Java读写文件方法总结(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149991.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号