프로그램에서 스트림이 열려 있는지 확인하는 방법이 있는지 궁금합니다.프로그램에서 이미 사용한 파일을 다시 작성하십시오.
나는 내 코드 중 일부를 사용하고 있는데, 내 목표는 같은 파일에 여러 번 쓰고, 지우고, 다시 쓸 때마다 여러 번 쓸 수 있도록하는 것이다. 그러나, 어딘가에,이 다른 그룹에 속한 코드가 스트림을 닫는 것을 잊어 버렸거나 자바가 처리 할 수 없다고 생각할 수도 있습니다. 빈 파일의 시작 부분이 아닌 파일의 끝 부분에 항상 씁니다. 삭제되지 않으며 프로그램에서 이미 열어 둔 경우 이름을 바꿀 수 없습니다.
오픈 스트림 문제인 경우 스트림을 닫고 싶습니다 (코드를 살펴본 결과 공개 스트림을 찾을 수없는 것 같습니다). 아니면 자바가 그것을 처리 할 수 없다면, 나를 위해 인스턴스를 다시 설정/죽일 수있는 좋은 방법 (파괴 방법을 만드는 외에)이 있습니까?
또는 어쩌면 ... 파일을 null로 설정하고 지우는 방법이 있습니까? 또는 파일을 열고 지우고 오프셋을 0으로 설정해야합니까? 이 도움이
public void writeToNewFile(String filePath, String data)
{
PrintWriter writer;
File file;
try
{
file = new File(filePath);
file.createNewFile();
writer = new PrintWriter(new FileWriter(file));
writer.println(data);
writer.flush();
writer.close();
}catch(Exception e){e.printStackTrace();}
writer = null;
file = null;
\\setting file & writer to null releases all the system resources and allows the files to be accessed again later
}
//this will write to end of file
public void writeToExistingFile(String filePath, String data)
{
PrintWriter writer;
File file;
try
{
file = new File(filePath);
if(!file.exists())
file.createNewFile();
writer = new PrintWriter(new FileWriter(file,true));
writer.println(data);
writer.flush();
writer.close();
}catch(Exception e){e.printStackTrace();}
writer = null;
file = null;
\\setting file & writer to null releases all the system resources and allows the files to be accessed again later
}
public String[] readFile(String filePath)
{
String data[];
Iterator<String> it;
ArrayList<String> dataHolder = new ArrayList<String>();
BufferedReader reader;
File file;
try
{
file = new File(filePath);
reader = new BufferedReader(new FileReader(file));
int lines = 0;
while(reader.ready())
{
lines++;
dataHolder.add(reader.readLine());
}
data = new String[lines];
it = dataHolder.iterator();
for(int x=0;it.hasNext();x++)
data[x] = it.next();
reader.close();
}catch(Exception e){e.printStackTrace();}
reader = null;
file = null;
\\setting file & reader to null releases all the system resources and allows the files to be accessed again later
return data;
}
public void deleteFile(String filePath)
{
File file;
try
{
file = new File(filePath);
file.delete();
}catch(Exception e){e.printStackTrace();}
file = null;
}
public void createDirectory(String directory)
{
File directory;
try
{
directory = new File(directory);
directoyr.mkDir();
}catch(Exception e){e.printStackTrace();}
directory = null;
}
희망 :
어떤 조언 당신에게 유용 할 수있는 좋은
시작으로 이것을 제공하겠습니다. 파일 쓰기는 기본적으로 추가 모드가 아니며 명시 적으로 설정해야합니다. 따라서 "파일 끝에 항상 씁니다"라고 언급 한 부분은이 다른 코드 또는 사용자가 추가 할 것이라고 말합니다. 소스 코드 및 오류 메시지가 환영합니다. – demongolem
파일을 이미 열었는지 확인하여 상황을 평가할 수 있도록이 SO 링크를 공유하겠습니다. http://stackoverflow.com/questions/1390592/java-check-if-file-is-already-open – demongolem
다른 코드가 추가 할 것을 말하고 있습니다. 내 목표는 코드를 수정하지 않는 것입니다 (업데이트를 릴리스하고 항상 코드를 변경해야 함). false로 설정하려고 시도했지만 파일을 작성하는 방식은 파일을 설정하지 않는 한 효과적으로 작성하지 못합니다. 참된. 파일을 지울 수있는 방법을 찾고, 어쩌면 지정된 위치에서 파일의 특정 번호를 씁니다 쓰기 메서드를 사용하여. 그러나, 나는 그것을 무엇으로 써야하는지 잘 모르겠다. nulls 또는 공백? @ demongolem 나는 그것이 가능하지 않을 수도 있다는 것을 제외하고는 그 링크가 어떻게 도움이되는지 잘 모르겠습니다. – Sean