이것은 나를 미치게합니다! 디렉토리의 파일 목록을 표시하는 패널이 있습니다. 목록은 벡터에 저장됩니다. 버튼을 클릭하면 같은 디렉토리에 새 파일이 만들어지고 목록을 새로 고쳐야합니다.디렉토리 목록이 새로 고쳐지지 않습니다
Dos에서 좋은 이전 DIR을 추가해도 Dos에서 파일을 볼 수 있지만 자바가 새 파일을 볼 수없는 이유를 모르겠습니다. 마치 새 파일을 볼 수 있고 Dos에서 볼 수는 있어도 새 파일이 보이지 않는 것입니다. 나는 약간의 시간 (수면, 수확량)을 주려고 노력했지만 아무 쓸모가 없다. 나는 또한 새로운 임시 파일에 복사하고 임시 읽기를 시도했지만 아무 소용이 없습니다. 다음은 일부 코드입니다 (관련없는 일부 선을 제거함).
public class Button extends EncapsulatedButton {
public Button()
{
super("button pressed");
}
public void actionPerformed(ActionEvent arg0) {
//removed function here where the new file is created in the directory
//remove call to DOS that regenerates /myFileList.txt after a new file was added in the directory
//at this point, DOS can see the new file and myFileList.txt is updated, however it is read by java without the update!!!!!
//now trying to read the directory after the new file was created
Vector data = new Vector<String>();
String s = null;
// Create the readers to read the file.
try {
File f = new File("/myFileList.txt");
BufferedReader stream = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
while((s = stream.readLine()) != null)
{
data.addElement(s.trim());
}
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
util();
}
void util(){
//giving it time is not helping
Thread.yield();
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
//get the file listing through java instead of DOS - still invisible
File fLocation = new File("/myDir");
File[] filesFound = fLocation.listFiles();
for (int i = 0; i < filesFound.length; i++) {
if (filesFound[i].isFile()) {
System.out.println("**********" + filesFound[i].getName());
}
}
//last resort: copy to a temp then read from there - still not good
try{
//copy to a temp file
File inputFile = new File("/myFileList.txt");
File outputFile = new File("/myFileList_temp.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
//read the copy to see if it is updated
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("/myFileList_temp.txt");
// Get the object of DataInputStream
DataInputStream in1 = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in1));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println ("Test file read: " + strLine);
}
//Close the input stream
in1.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
나는 모든 리드를 부탁드립니다. 고맙습니다. 이 같은
myFileList.txt의 lokks은 :
myline1
myline2
myline3
myline4가에 표시, 폴더에 새 파일을 추가 한 후, 다음 판독되고 패널에 표시됩니다.
정확히이 파일을 읽으려고하십니까? 정말 루트 디렉토리?/myDir은 무엇입니까? – FRotthowe
파일과 디렉터리는 런타임에 동적으로 생성됩니다. 여기에 제시된 위치는 로컬 디렉토리의 더미 대체품입니다. 코드베이스는 꽤 복잡하지만 좋은 근사값이라고 생각했습니다. 어쨌든 잘못된 파일이나 잘못된 디렉토리를 읽는 경우는 아닙니다. 텍스트 편집기에서 열리는 파일을 볼 수 있으며 새로운 줄이 추가되었습니다. – alex
왜 하나님의 이름으로, 단순히 디렉토리 내용을 나열하는 파일 대신에 File 클래스에이 방법이있는 디렉토리를 읽지 않습니까?) – Ingo