디렉토리를 탐색하여 "rels.txt"라는 파일의 인스턴스를 모두 찾으려고합니다. 각 인스턴스에 대해 파일로 이동하여 개별 파일을로드하려고합니다. 값을 파일에서 Java의 HashMap으로 변환합니다. 아래는 내가 작업하고있는 "rels.txt"파일 중 하나의 내용은 다음과 같습니다파일을 Java의 HashMap으로 스캔
텍스트 파일 : 여기
rId8,image2
rId13,image5
rId7,image1
rId12,image4
rId17,image8
rId15,image7
rId9,image3
rId14,image6
내가 지금까지 그은을 통과하도록되어있는 자바 코드 디렉토리에 저장하고 각각의 값을 HashMap에 저장하고, HashMap을 인쇄 한 다음 지우십시오 (디렉토리 내의 다른 "rels.txt"파일에서 새로운 값 세트를 저장할 수 있도록).
자바 코드 :
private void traverse(File directory) throws FileNotFoundException
{
//Get all files in directory
File[] files = directory.listFiles();
for (File file : files)
{
if (file.getName().equals("rels.txt"))
{
Scanner scan = new Scanner(file).useDelimiter(",");
while (scan.hasNextLine())
{
String id;
String image;
id = scan.next();
image = scan.next();
imageMap.put(id, image);
}
System.out.println(imageMap);
imageMap.clear();
}
else if (file.isDirectory())
{
//It's a directory so (recursively) traverse it
traverse(file);
}
}
}
이 내가 점점 오전 오류/출력 내가 잘못 가고 어디 아주 확실하지 않다.
오류이 코드/출력
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at XMLTagParser.traverse(XMLTagParser.java:153)
at XMLTagParser.traverse(XMLTagParser.java:198)
at XMLTagParser.traverse(XMLTagParser.java:198)
at XMLTagParser.<init>(XMLTagParser.java:27)
at IPDriver.main(IPDriver.java:21)
{image26
rId19=image9
rId31, image15
rId33=image23
rId38, image29
rId21=image11
rId34, image12
rId27=image17
rId30, image28
rId16=image6
rId20, image14
rId32=image22
rId37, image2
rId17=image7
rId25, rId13=image3
rId18, image20
rId35=image25
, image8
rId26=image16
rId39, image24
rId7=image1
rId12, image21
rId14=image4
rId22, image10
rId29=image19
rId24, image13
rId28=image18
rId36, image27
rId15=image5
rId23}
, 나는 본질적으로 내 프로젝트의 다른 부분에 대한 HashMap에 전화를 걸 수 있으려면 내가이 RID 값 중 하나를 참조 할 때, 내가 얻을 수 있습니다 대응하는 이미지 치 누구나 내가 잘못했거나 더 효율적인 솔루션 일 수있는 아이디어가 있습니까? 미리 감사드립니다. Friedrik에
작업 자바 코드 감사 :
private void traverse(File directory) throws FileNotFoundException
{
//Get all files in directory
File[] files = directory.listFiles();
for (File file : files)
{
if (file.getName().equals("rels.txt"))
{
Scanner scan = new Scanner(file);
while (scan.hasNextLine())
{
String[] line = scan.nextLine().split(",");
imageMap.put(line[0], line[1]);
}
System.out.println(imageMap);
imageMap.clear();
}
else if (file.isDirectory())
{
//It's a directory so (recursively) traverse it
traverse(file);
}
}
}
내가 무슨 말을하는지 알 겠지만 구분 기호에 추가 인수를 추가하려면 어떻게해야합니까? (그것으로 처음으로 일하면서, 죄송합니다.) –
어쩌면 regex "[,] [\ n]"에있을 수 있습니다. 확실하지 않습니다. 네가 원하면 내가 할 수있는 또 다른 방법을 줄 수있어. – Friedrik
네, 물론, 어떤 제안을 열어 –