동시에 두 곳에서 파일을 읽고 싶습니다. 또한 효율성을 위해 버퍼 된 I/O 스트림을 사용하려고합니다. 내 자신의 주어진 자바 API에 sth을 해결하려했지만 작동하지 않습니다. 아무도 도와 줄 수 없나요? 외부 병합 정렬을 위해 필요합니다. 도와 주셔서 감사합니다!Java의 서로 다른 두 위치에서 동일한 파일에 액세스하는 방법
답변
RandomAccessFile
을 작성해야합니다. 이는 기본적으로 Java의 C의 메모리 맵핑 파일과 동일합니다. 이의 I found an example
:
try {
File file = new File("filename");
// Create a read-only memory-mapped file
FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)roChannel.size());
// Create a read-write memory-mapped file
FileChannel rwChannel = new RandomAccessFile(file, "rw").getChannel();
ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int)rwChannel.size());
// Create a private (copy-on-write) memory-mapped file.
// Any write to this channel results in a private copy of the data.
FileChannel pvChannel = new RandomAccessFile(file, "rw").getChannel();
ByteBuffer pvBuf = roChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int)rwChannel.size());
} catch (IOException e) {
}
편집, 당신은 당신이 건너 뛰고 아래 파일을 통해 할 수있는 유일한 방법입니다 RandomAccessFile
를 사용할 수 없습니다 말했다. 파일이 없으면 파일을 순차적으로 읽어야하지만, 그렇다고해서 동일한 파일에 대해 여러 개의 포인터를 열어 읽을 수는 없습니다.
다음 테스트/샘플을 함께 넣으면 다른 읽기 포인터로 "두 번"파일을 열고 파일의 두 부분을 순차적으로 합할 수 있음을 분명히 보여줍니다. 당신이 랜덤 액세스 필요하면 다시, 당신은 RandomAccessFile
를 사용해야하며, 그것이 내가 좋을 것 무엇, 그러나 여기 당신은 간다 :
public class FileTest {
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException{
File temp = File.createTempFile("asfd", "");
BufferedWriter wrt = new BufferedWriter(new FileWriter(temp));
int testLength = 10000;
int numWidth = String.valueOf(testLength).length();
int targetSum = 0;
for(int i = 0; i < testLength; i++){
// each line guaranteed to have a good number of characters for our test
wrt.write(String.format("%0"+ numWidth +"d\n", i));
targetSum += i;
}
wrt.close();
BufferedReader rdr1 = new BufferedReader(new FileReader(temp));
BufferedReader rdr2 = new BufferedReader(new FileReader(temp));
rdr2.skip((numWidth+1)*testLength/2); // skip first half of the lines
Summer sum1 = new Summer(rdr1, testLength/2);
Summer sum2 = new Summer(rdr2, testLength/2);
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<Integer> halfSum1 = executor.submit(sum1);
Future<Integer> halfSum2 = executor.submit(sum2);
System.out.println("Total sum = " + (halfSum1.get() + halfSum2.get()) + " reference " + targetSum);
rdr1.close();
rdr2.close();
temp.delete();
}
private static class Summer implements Callable<Integer>{
private BufferedReader rdr;
private int limit;
public Summer(BufferedReader rdr, int limit) throws IOException{
this.rdr = rdr;
this.limit = limit;
}
@Override
public Integer call() throws Exception {
System.out.println(Thread.currentThread().getName() + " started " + System.currentTimeMillis());
int sum = 0;
for(int i = 0; i < limit; i++){
sum += Integer.valueOf(rdr.readLine());
// uncomment to see interleaving of threads:
//System.out.println(Thread.currentThread().getName());
}
System.out.println(Thread.currentThread().getName() + " finished " + System.currentTimeMillis());
return sum;
}
}
}
오, 그래, 나는 기억지도가 허용되지 않는다 :(그것은 대학 과제를위한 것이고, 나의 유니는 약간 지체가 있으며, 메모리 맵이 허용되는 경우 메모리를 제한하는 방법을 모르고있다 .. – nivwusquorum
"제한 기억"? –
마치 두 개의 독립적 인 파일 인 것처럼?
File inputFile = new File("src/SameFileTwice.java");
BufferedReader in1 = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
BufferedReader in2 = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
try {
String strLine;
while ((strLine = in1.readLine()) != null && (strLine = in2.readLine()) != null) {
System.out.println(strLine);
}
} finally {
in1.close();
in2.close();
}
- 1. 동일한 도메인의 다른 위치에서 WordPress를 실행하는 방법
- 2. Java에서 Buffered IO를 사용하여 파일의 다른 두 위치에서 읽는 방법
- 3. 동일한 코드가 서로 다른 두 가지 앱에서 서로 다른 두 가지 작업을 수행하고 있습니까?
- 4. axis2 : 동일한 이름을 가진 두 개의 서로 다른 웹 서비스
- 5. 동일한 파일에 액세스하는 Python 다중 스레드
- 6. 두 개의 서로 다른 배열의 동일한 키에있는 서로 다른 값의 차이 백분율
- 7. 배치 파일에 액세스하는 방법?
- 8. XML 파일에 액세스하는 방법?
- 9. 두 개의 서로 다른 접촉을 감지하는 방법
- 10. 하나의 파일에 두 개의 서로 다른 파일 형식이 있습니까?
- 11. 스토리지 시스템은 같은 위치에서 서로 다른 쓰기 스트림을 어떻게 처리합니까?
- 12. 엑셀 파일에 액세스하는 방법
- 13. 서로 다른 두 명의 사용자가 서로 다른 응용 프로그램에서 동일한 Facebook 타사 ID를 사용할 수 있습니까?
- 14. 하나의 쿼리에서 동일한 테이블의 서로 다른 두 레코드의 부분을 반환하십시오.
- 15. 서로 다른 스타일의 두 페이지에 동일한 콘텐츠를 표시하려면 어떻게해야합니까?
- 16. 두 개의 서로 다른 URL을 사용하여 동일한 git 리모컨으로 푸시
- 17. jQuery는 서로 다른 두 이벤트에 대해 동일한 코드를 실행합니다.
- 18. 동일한 테이블에있는 두 개의 서로 다른 값을 기준으로 카운터를 가져옵니다.
- 19. 동일한 도메인에 두 개의 서로 다른 세션이 있습니다.
- 20. 동일한 서버에있는 서로 다른 두 개의 DB에있는 테이블의 외래 키
- 21. C++/VS2005 : 두 개의 서로 다른 .cpp 파일에 동일한 클래스 이름 정의
- 22. 동일한 사용자의 동일한 사이트에 대해 서로 다른 두 개의 열린 ID를 얻는 방법
- 23. 두 쿠키는하지만, 서로 다른 값
- 24. 두 개의 서로 다른 금액
- 25. NUnit 테스트에서 프로젝트 파일에 액세스하는 방법
- 26. 동일한 파일에 동시에 액세스하는 여러 세션 중지
- 27. 두 폼을 서로 참조하는 방법
- 28. 서로 다른 ViewControllers/EAGLViews에서 동일한 EAGLContext 사용
- 29. ASP.NET MVC에서 서로 다른 모델, 동일한 데이터
- 30. ASP는 두 개의 서로 다른 씨앗
왜 동시에 두 위치에서 파일을 읽으시겠습니까? io 증기의 코드/예외 스택 추적이 작동하지 않습니다. – Jeffrey
기본적으로 RandomAccessFile을 사용하려고 시도한 다음 랩핑했습니다. new DataInputStream (new BufferedInputStream (new FileInputStream (file.getFD())))); 여기서 파일은 임의 액세스 파일입니다. 내가 다른 곳에서 파일 블록을 액세스하기 시작할 때마다 나는 RandomAccessFile을 찾고 새로운 래퍼를 만들었다. – nivwusquorum