이 기사에서는 문자 스트림 양식을 기반으로 한 Java 읽기 및 쓰기 데이터의 두 가지 구현 방법에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
첫 번째 방법 : 하나의 문자별로 작업을 읽고 쓰기 (자세한 내용을위한 코드 주석 및 무료 보충제)
패키지 iodemo; import java.io.fileReader; import java.io.filewriter; import java.io.ioexception; public class copyfiledemo { / ** * @param args * @throws ioexception * / public static void main (strows [] args) {filereader fr = new filereader ( "); filewriter fw = 새로운 filewriter ( "demo1.txt"); int ch = 0; while ((ch = fr.read ())! = -1) {// fw.write (ch)를 읽는 단일 문자; // 쓰기를위한 단일 문자} fw.close (); fr.close (); }} 두 번째 방법 : 버퍼를 사용자 정의하고 read(char buf[]) 메소드를 사용하면이 방법이 더 효율적입니다.
패키지 iodemo; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; public class copyfiledemo2 {private static final int buffer_size = 1024; / ** * @param args */ public static void main (String [] args) {filereader fr = null; filewriter fw = null; try {fr = new filereader ( "demo.txt"); // 프로젝트가 위치한 디렉토리 fw = new FileWriter ( "demo2.txt"); char buf [] = new char [buffer_size]; int len = 0; while ((len = fr.read (buf))! = -1) {fw.write (buf, 0, len); }} catch (Exception e) {// todo : handing exception} and morne {if (fr! = null) {try {fr.close (); } catch (ioException e) {System.out.println ( "읽기 및 쓰기 실패"); }} if (fw! = null) {try {fw.close (); } catch (ioException e) {System.out.println ( "읽기 및 쓰기 실패"); }}}}}}}}Java 알고리즘에 대한 자세한 내용은이 사이트에 관심이있는 독자가 주제를 볼 수 있습니다. "Java 파일 및 디렉토리 운영 기술 요약", "Java 데이터 구조 및 알고리즘에 대한 튜토리얼", "Java Operation Dom Node Skills 요약"및 "Java Cache 운영 기술 요약".
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.