Java io 文件操作
1、io分类图
2、java io基本操作:
1 /** 2 * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 3 * 一次读一个字节 4 * @param fileName 5 */ 6 public static void readFileByBytes(String fileName) { 7 try { 8 File file = new File(fileName); 9 InputStream inputStream = new FileInputStream(file); 10 int readByte = 0; 11 while ((readByte = inputStream. 12 read()) != -1) { 13 System.out.write(readByte); 14 } 15 inputStream.close(); 16 } catch (FileNotFoundException e) { 17 e.printStackTrace(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 }
1 /** 2 * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 3 * 一次读多个字节 4 * @param fileName 5 */ 6 public static void readFileByMoreBytes(String fileName) { 7 File file = new File(fileName); 8 try { 9 InputStream inputStream = new FileInputStream(file); 10 byte[] reads = new byte[100]; 11 int byteread = 0; 12 13 while ((byteread = inputStream.read(reads)) != -1) { 14 System.out.write(reads,0,byteread); 15 } 16 } catch (FileNotFoundException e) { 17 e.printStackTrace(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 }
/** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 * 一次读一字节 * @param fileName */ public static void readFileByChars(String fileName) { File file = new File(fileName); try { InputStream inputStream = new FileInputStream(file); InputStreamReader reader = new InputStreamReader(inputStream); int temp = 0; while ((temp = reader.read()) != -1) { // if ((char)temp != '\r') { System.out.print((char) temp); // } } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 * 一次读多个字节 * @param fileName */ public static void readFileByMoreChars(String fileName) { File file = new File(fileName); try { InputStreamReader reader = new InputStreamReader(new FileInputStream(file)); char[] chars = new char[100]; int temp = 0; while ((temp = reader.read(chars)) != -1) { if ((temp == chars.length) && (chars[chars.length - 1] != '\r')) { System.out.print(temp); } else { for (int i = 0; i < temp; i++) { if (chars[i] != '\r') { System.out.print(chars[i]); } else { continue; } } } } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * 以行为单位读取文件,常用于读面向行的格式化文件 * @param fileName */ public static void readFileByLine(String fileName) { File file = new File(fileName); try { BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String temp = null; int line =1; while ((temp = bufferedReader.readLine()) != null) { System.out.println("line : " + line + " : " + temp); line++; } bufferedReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * 随机读取文件内容 * @param fileName */ public static void readFileByRandomAccess(String fileName) { try { RandomAccessFile accessFile = new RandomAccessFile(fileName,"r"); long fileLength = accessFile.length(); int beginIndex = fileLength > 10 ? 10 : 0; accessFile.seek(beginIndex); byte[] bytes = new byte[10]; int temp = 0; while ((temp = accessFile.read(bytes)) != -1) { System.out.write(bytes,0,temp); } accessFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * 对文件进行追加 * @param fileName * @param content */ public static void appendMethodA(String fileName,String content) { try { RandomAccessFile accessFile = new RandomAccessFile(fileName,"rw"); long length = accessFile.length(); accessFile.seek(length); accessFile.write(content.getBytes()); accessFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void appendMethodB(String fileName,String content) { try { FileWriter writer = new FileWriter(fileName,true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
只有十分努力,看上去才游刃有余