Java의 읽기 및 쓰기 파일 방법은 많은 작업에 사용된다고 생각합니다. Java의 읽기 및 쓰기 파일 방법을 사용하여 데이터 입력 및 출력을 처리했습니다. 이는 실제로 매우 편리합니다. 그러나 내 기억은 정말 걱정입니다. 여러 번, 글을 쓰는 방법을 기억할 수 없기 때문에 Java 코드는 정말 작기 때문에 더 많은 연습을해야합니다. 요약은 다음과 같습니다. 앞으로의 측면에 중점을 둘 것입니다.
자바 읽기 파일
패키지 천재 바보 꿈; import java.io.bufferedReader; import java.io.file; import java.io.fileInputStream; import java.io.filereader; import java.io.ioexception; import java.io.inputStreamReader; import java.io.inputStreamReader; import java.io.randomacccsefile; javaio { / ** * 운영 체제, GBK 등의 기본 인코딩 메소드, 비 utf8 * / / ** * 바이트에서 파일 내용을 읽습니다. 종종 그림, 이미지, 사운드 등과 같은 이진 파일을 읽는 데 사용됩니다. fileInputStream in = null; try {system.out.println ( "바이트로 파일 읽기, 한 번에 하나의 바이트 읽기 :"); in = new FileInputStream (파일); int temp = 0; while ((temp = in.read ())! = -1) {system.out.println (temp); } in.close (); } catch (ioexception e) {e.printstacktrace (); 반품 ; } try {system.out.println ( "바이트로 파일을 읽고 한 번에 여러 바이트를 읽습니다 :"); 바이트 [] temp = 새로운 바이트 [100]; int byteread = 0; in = new FileInputStream (파일); javaio.showavailableBytes (in); while ((byteread = in.read (temp))! = -1) {system.out.write (temp, 0, byteread); }} catch (예외 e1) {e1.printstacktrace (); } 마침내 {if (in! = null) {try {in.close (); } catch (ioexception e1) {}}}}} / ** * 문자의 파일 읽기, 텍스트, 숫자 등의 파일을 읽는 데 종종 사용됩니다. * / public static void void readfilebychar (string filename) {file file = 새 파일 (filename); 독자 독자 = null; try {system.out.println ( "문자의 파일 내용 읽기, 한 번에 한 바이트를 읽습니다 :"); // inputStreamReader 클래스 : 바이트를 문자로 변환하기위한 브리지 reader = 새 입력 스트림 리더 (new FileInputStream (file)); int 온도; while ((temp = reader.read ())! = -1) {if ((char) temp)! = '/r') {system.out.println ((char) temp); }} reader.close (); } catch (예외 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 <charread; i ++) {if (temp [i] == '/r') {break; } else {system.out.println (temp [i]); }}}}}}} catch (예외 e) {e.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioexception e) {}}}}} / *** 동작 단위의 파일 읽기, 종종 행이있는 형식의 파일을 읽는 데 사용됩니다* / public static void void readfilebyline (string filename) {file file = new File (filename); bufferedReader reader = null; try {System.out.println ( "동작 단위의 파일 내용 읽기, 한 번에 하나의 전체 라인을 읽으십시오 :"); reader = new bufferedReader (New Filereader (파일)); 문자열 온도 = null; int line = 1; while ((temp = reader.readline ())! = null) {system.out.println ( "line" + line + ":" + temp); 라인 ++; } reader.close (); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (reader! = null) {try {reader.close (); } catch (ioexception e) {}}}} / *** 무작위 읽기 파일 내용** / public static void readfilebyrandomAccess (String filename) {randomaccessfile randomfile = null; try {system.out.println ( "랜덤 컨텐츠를 읽습니다"); randomfile = new randomaccessfile (filename, "r"); long filelength = randomfile.length (); int beginIndex = (FilElength> 4? 4 : 0); randomfile.seek (beginindex); 바이트 [] 바이트 = 새로운 바이트 [10]; int byteread = 0; while ((bytead = randomfile.read (bytes))! = -1) {system.out.write (bytes, 0, byteread); }} catch (ioexception e) {e.printstacktrace (); } morne {if (randomfile! = null) {try {randomfile.close (); } catch (ioexception e) {}}}} 개인 정적 무효 ShowaVailableBytes (inputStream in) {try {system.out.println ( "현재 바이트 입력 스트림의 바이트 수는 :" + in.availeable ()); } 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는 파일을 작성합니다
패키지 천재 바보 꿈; import java.io.bufferedwriter; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.filewriter; import java.io.ioexception; public class javaio ioException {String Path = "e : // 천재 바보 꿈 // java"; 파일 파일 = 새 파일 ( "e : // 천재 바보 꿈 // java", "baiyishaonian.txt"); if (! file.exists ()) {try {file.createnewfile (); } catch (ioexception e) {e.printstacktrace (); }} / *** java에서 파일을 작성하는 세 가지 방법** / fileoutputStream fos = null; 완충 작가 bw = null; filewriter fw = null; int 값 = 1000; try {fos = new FileOutputStream (새 파일 (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 :" + (end-begin)); fos.close (); bw = new bufferedWriter (new outputStreamWriter (new FileOutputStream (새 파일 (path+"b.txt"), "UTF8")); 시작 = System.CurrentTimeMillis (); for (int i = 1; i <= value; i ++) {bw.write (5); bw.newline (); } bw.close (); END = SYSTEM.CURRENTTIMEMILLIS (); System.out.println ( "버퍼링 라이터의 thecosttime is :" + (end-begin)); fw = 새로운 filewriter (path+"fw.txt"); 시작 = System.CurrentTimeMillis (); for (int i = 1; i <= value; i ++) {fw.write (5); } fw.close (); END = SYSTEM.CURRENTTIMEMILLIS (); System.out.println ( "Filewriter의 thecosttime :" + (end-begin)); } catch (예외 e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } 마침내 {try {fos.close (); // fileoutputStream bw.close (); // bufferedWriter fw.close (); // filewriter} catch (예외 e) {e.printstacktrace (); }}}}Java 읽기 및 쓰기 파일 방법 (권장)의 위의 요약은 내가 공유하는 모든 내용입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.