星空网 > 软件开发 > Java

Java读写文件方法总结

Java读写文件方法总结

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

Java读文件

 1 package 天才白痴梦; 2  3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.RandomAccessFile; 11 import java.io.Reader; 12  13 public class JavaIO { 14    15   /** 16    * 采用的是操作系统底层默认的编码方式,GBK等,非UTF8 17    * */ 18    19   /** 20    * 以字节为单位读取文件内容,常用于读取二进制文件,比如图片、影像、声音等文件 21    * */ 22   public static void readFileByBytes(String filename) { 23     File file=new File(filename); 24     FileInputStream in=null; 25     try { 26       System.out.println("以字节为单位读取文件,一次读一个字节: "); 27       in=new FileInputStream(file); 28       int temp=0; 29       while ((temp=in.read()) != -1) { 30         System.out.println(temp); 31       } 32       in.close(); 33     } catch (IOException e) { 34       e.printStackTrace(); 35       return ; 36     } 37     try { 38       System.out.println("以字节为单位读取文件,一次读多个字节: "); 39       byte[] temp=new byte[100]; 40       int byteread=0; 41       in=new FileInputStream(file); 42       JavaIO.showAvailableBytes(in); 43       while ((byteread=in.read(temp)) != -1) { 44         System.out.write(temp,0,byteread); 45       } 46     } catch (Exception e1) { 47       e1.printStackTrace(); 48     } finally { 49       if (in != null) { 50         try { 51           in.close(); 52         } catch (IOException e1) { 53            54         } 55       } 56     } 57   } 58   /** 59    * 以字符为单位读取文件,常用于读文本,数字等类型的文件 60    * */ 61   public static void readFileByChar(String filename) { 62     File file=new File(filename); 63     Reader reader=null; 64     try { 65       System.out.println("以字符为单位读取文件内容,一次一个字节:"); 66       //InputStreamReader类:是字节向字符转换的桥梁 67       reader=new InputStreamReader(new FileInputStream(file)); 68       int temp; 69       while ((temp=reader.read()) != -1) { 70         if (((char)temp) != '\r') { 71           System.out.println((char)temp); 72         } 73       }  74       reader.close(); 75     } catch (Exception e) { 76       e.printStackTrace(); 77     } 78     try { 79       System.out.println("以字符为单位读取文件内容,一次读多个字节: "); 80       char[] temp=new char[30]; 81       int charread=0; 82       reader=new InputStreamReader(new FileInputStream(filename)); 83       while ((charread=reader.read(temp)) != -1) { 84         if ((charread == temp.length) && (temp[temp.length-1]!='\r')) { 85           System.out.println(temp); 86         } else { 87           for (int i=0; i<charread; i++) { 88             if (temp[i] == '\r') { 89               break; 90             } else { 91               System.out.println(temp[i]); 92             } 93           } 94         } 95       } 96     } catch (Exception e) { 97       e.printStackTrace(); 98     } finally { 99       if (reader != null) {100         try {101           reader.close();102         } catch (IOException e) {103           104         }105       }106     }107   }108   /**109    * 以行为单位读取文件,常用于读面向行的格式化文件110    * */111   public static void readFileByLine(String filename) {112     File file=new File(filename);113     BufferedReader reader=null;114     try {115       System.out.println("以行为单位读取文件内容,一次读一整行: ");116       reader=new BufferedReader(new FileReader(file));117       String temp=null;118       int line=1;119       while ((temp=reader.readLine()) != null) {120         System.out.println("line " + line + ": " + temp);121         line++;122       }123       reader.close();124     } catch (IOException e) {125       e.printStackTrace();126     } finally {127       if (reader != null) {128         try {129           reader.close();130         } catch (IOException e) {131           132         }133       }134     }135   }136   /**137    * 随机读取文件内容138    * */139   public static void readFileByRandomAccess(String filename) {140     RandomAccessFile randomfile=null;141     try {142       System.out.println("随机读取一段文件内容");143       randomfile=new RandomAccessFile(filename,"r");144       long fileLength=randomfile.length();145       int beginIndex=(fileLength > 4 ? 4 : 0);146       randomfile.seek(beginIndex);147       byte[] bytes=new byte[10];148       int byteread=0;149       while ((byteread=randomfile.read(bytes)) != -1) {150         System.out.write(bytes,0,byteread);151       }152     } catch (IOException e) {153       e.printStackTrace();154     } finally {155       if (randomfile != null) {156         try {157           randomfile.close();158         } catch (IOException e) {159           160         }161       }162     }163   }164   private static void showAvailableBytes(InputStream in) {165     try {166       System.out.println("当前字节输入流中的字节数为:" + in.available());167     } catch (IOException e) {168       e.printStackTrace();169     }170   }171   public static void main(String[] args) {172     String filename="E:\\BaiYiShaoNian.txt";173     JavaIO.readFileByBytes(filename);174     JavaIO.readFileByChar(filename);175     JavaIO.readFileByLine(filename);176     JavaIO.readFileByRandomAccess(filename);177   }178 }

Java写文件

 1 package 天才白痴梦; 2  3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.OutputStreamWriter;10 11 public class JavaIO2 {12 13   public static void main(String[] args) throws IOException {14     String Path="E:\\天才白痴梦\\JAVA";15     File file=new File("E:\\天才白痴梦\\JAVA","BaiYiShaoNian.txt");16     if (!file.exists()) {17       try {18         file.createNewFile();19       } catch (IOException e) {20         e.printStackTrace();21       }22     }23     /**24      * Java写入文件的三种方法25      * */26     FileOutputStream fos=null;27     BufferedWriter bw=null;28     FileWriter fw=null;29     int value=1000;30     31     try {32       fos=new FileOutputStream(new File(Path+"fos.txt"));33       long begin=System.currentTimeMillis();34       for (int i=1; i<=value; i++) {35         fos.write(5);36       }37       long end=System.currentTimeMillis();38       System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));39       fos.close();40       41       bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));42       begin=System.currentTimeMillis();43       for (int i=1; i<=value; i++) {44         bw.write(5);45         bw.newLine();46       }47       bw.close();48       end=System.currentTimeMillis();49       System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));50       51       fw=new FileWriter(Path+"fw.txt");52       begin=System.currentTimeMillis();53       for (int i=1; i<=value; i++) {54         fw.write(5);        55       }56       fw.close();57       end=System.currentTimeMillis();58       System.out.println("TheCostTime of FileWriter is : " + (end-begin));59       60       61     } catch (Exception e) {62       // TODO Auto-generated catch block63       e.printStackTrace();64     } finally {65       try {66         fos.close(); //FileOutputStream67         bw.close(); //BufferedWriter68         fw.close(); //FileWriter69       } catch (Exception e) {70         e.printStackTrace();71       }72     }73     74   }75 }

Java读写文件方法总结

 




原标题:Java读写文件方法总结

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流