2013年9月23日星期一

FileWriter finished file, use System.exit (0); File will be deleted

Such as the title , the first call writeToIniFile, which delete the file , and then re-write . The problem is only one execution to System.exit (0) Just write the file was deleted, what is the reason ?


public void writeToIniFile() throws IOException {
System.out.println("start to write info into news.ini");

String newsIni = USBConfig.drivePath + USBConfig.INIT_NEW_FOLDER + "\\news.ini";
File f = new File(newsIni);
if(f.exists()){
f.delete();
}
f.createNewFile();
int sum = this.getnewsList().size();
List<String> lines = new ArrayList<String>();
lines.add("[news]");
lines.add("sum="+sum);
StringBuilder line = new StringBuilder("news");
for(int i=0; i < sum; i++){
News n = this.getnewsList().get(i);
line.append(i).append("=").append(n.content);
lines.add(line.toString());
line = new StringBuilder("news");
}
System.out.println("the lines in memory ===============");
for(String s : lines){
System.out.println(s);
}
System.out.println("the lines in memory ===============");

FileReaderUtils.writeToFile(lines, f);

}



public static void writeToFile(List<String> lines, File ff) throws IOException{
System.out.println("writeToFile function started.");
File f = new File(ff.getAbsolutePath());
f.createNewFile();
String encoding = code(f);
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fos,
encoding);
// FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(writer);
for(String s : lines){
// bw.append(s);
bw.write(s);
bw.newLine();
bw.flush();
}
bw.flush();
writer.flush();
fos.flush();
bw.close();
writer.close();
fos.close();
System.out.println("writeToFile function finished.");
}

------ Solution ------------------------------------- -------
an API to create a file to look at it, there are many of you that are creating a temporary file .

also possible that the file did not close

all try
------ Solution --------------------------------- -----------
procedure to delete files you place it ?
such as determining whether there is , if there is , then it is deleted, and then re- created.
time to pay attention here to delete using onDelete, do not use onExistDelete. You can see the specific API.
------ For reference only -------------------------------------- -
FileReaderUtils.writeToFile (lines, f); calling this method will create a f a .
------ For reference only -------------------------------------- -
in the code you posted did not see System.exit (0)
------ For reference only ------------------- --------------------


before is this , or not :

public static void writeToFile(List<String> lines, File f) throws IOException{
System.out.println("writeToFile function started.");
// File f = new File(ff.getAbsolutePath());
// f.createNewFile();
String encoding = code(f);
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fos,
encoding);
// FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(writer);
for(String s : lines){
// bw.append(s);
bw.write(s);
bw.newLine();
bw.flush();
}
bw.flush();
writer.flush();
fos.flush();
bw.close();
writer.close();
fos.close();
System.out.println("writeToFile function finished.");
}

------ For reference only ----------------------------------- ----


I will call elsewhere System.exit (0); is a closed process , I ordered the execution of the System.exit (0); newly generated files are deleted . They do not perform to System.exit (0); the file has been in the .
------ For reference only -------------------------------------- -
not know you System.exit (0); placed where , System.exit (0); was clear the entire jvm 's content, and if you flush or close the file before using System.exit (0); should this situation occurs you . System.exit (0); placed finally inside or last.

------ For reference only ---------------------------------- -----

I and other procedures are performed to System.out.println ("writeToFile function finished."); ended up here , and then a button . Button binding event is System.exit (0);
As long as I do not point the button , the file contents are written, and the file has also been . Too strange.

------ For reference only ---------------------------------- -----


File API then point , I have tried it . Feeling is not close properly and streaming on , I'll take a closer look
------ For reference only ------------------------ ---------------
must use System.exit (0) to end the program it ?
------ For reference only -------------------------------------- -
public void writeToIniFile () throws IOException {
System.out.println ("start to write info into news.ini");

String newsIni = USBConfig.drivePath + USBConfig.INIT_NEW_FOLDER + "\ \ news.ini";
File f = new File (newsIni);
if (f.exists ()) {
f.delete ();
}
f.createNewFile ();
int sum = this.getnewsList (). size ();
List lines = new ArrayList ();
lines.add ("[news]");
lines.add ("sum =" + sum);
StringBuilder line = new StringBuilder ("news");
for (int i = 0; i News n = this.getnewsList (). get (i);
line.append (i). append ("="). append (n.content) ;
lines.add (line.toString ());
line = new StringBuilder ("news");
}
System.out.println ("the lines in memory ============== = " ) ;
for (String s: lines) {
System.out.println (s);
}
System.out.println ("the lines in memory ============== = " ) ;

FileReaderUtils.writeToFile (lines, f);

}
System.exit (0) You should write in this class inside it , you write in here :
I always perform other procedures to System.out.println ("writeToFile function finished."); ended up here , and then a button . Button binding event is System.exit (0); is written inside the function . Calling the program has pulled out . . .
------ For reference only -------------------------------------- -
System.exit this procedure is not called when the file deletion function .... or a signal to deal with !
------ For reference only -------------------------------------- -

reason to find that onExistDelete issue.

没有评论:

发表评论