특정 경로에서 파일을 읽으려면 java에 옵션이 있는지 궁금합니다. C:\test1.txt
C:\test1.txt
의 내용은 메모리에있는 파일 내용을 변경하고 D:\test2.txt
으로 복사합니다. 변화하지만, 영향을받는 파일이 D:\test2.txt
파일을 덮어 쓰는 대신 다른 파일에 쓰기
감사
염기성 용액으로특정 경로에서 파일을 읽으려면 java에 옵션이 있는지 궁금합니다. C:\test1.txt
C:\test1.txt
의 내용은 메모리에있는 파일 내용을 변경하고 D:\test2.txt
으로 복사합니다. 변화하지만, 영향을받는 파일이 D:\test2.txt
파일을 덮어 쓰는 대신 다른 파일에 쓰기
감사
염기성 용액으로될 것입니다, 당신은 하나의 FileInputStream
에서 덩어리로 읽을과 FileOutputStream
쓸 수 있습니다 :
import java.io.*;
class Test {
public static void main(String[] _) throws Exception{
FileInputStream inFile = new FileInputStream("test1.txt");
FileOutputStream outFile = new FileOutputStream("test2.txt");
byte[] buffer = new byte[128];
int count;
while (-1 != (count = inFile.read(buffer))) {
// Dumb example
for (int i = 0; i < count; ++i) {
buffer[i] = (byte) Character.toUpperCase(buffer[i]);
}
outFile.write(buffer, 0, count);
}
inFile.close();
outFile.close();
}
}
명시 적으로 전체 파일을 메모리에 넣으려는 경우 DataInputStream
에 입력 한 내용을 바꿀 수 있으며 File.length()
을 사용한 후 readFully(byte[])을 사용하면 파일 크기를 알 수 있습니다.
내가 생각하기에, 가장 쉬운 방법은 파일을 읽고 작가에게 쓸 때 Scanner class을 사용하는 것입니다.
Here are some nice examples 다른 자바 버전.
apache commons lib을 사용하여 파일을 읽고 쓰거나 복사 할 수도 있습니다.
public static void main(String args[]) throws IOException {
//absolute path for source file to be copied
String source = "C:/sample.txt";
//directory where file will be copied
String target ="C:/Test/";
//name of source file
File sourceFile = new File(source);
String name = sourceFile.getName();
File targetFile = new File(target+name);
System.out.println("Copying file : " + sourceFile.getName() +" from Java Program");
//copy file from one location to other
FileUtils.copyFile(sourceFile, targetFile);
System.out.println("copying of file from Java program is completed");
}
'test2.txt'에 씁니다. –
@Tichodroma : 정확하게. – ankurtr
즉, 나는 그것을 비행 중에 만들어야 함을 의미합니다. –