2016-12-01 37 views
0

当我没有放置命令行参数时,我可以写入文件,但是每次放入命令行参数时都不会写入。即使我甚至没有使用命令行参数。使用命令行参数时无法写入文件

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 



public class Test { 

    public Test() throws IOException { 

     String content = "writing..."; 

     File file = new File("sample.txt"); 

     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write(content); 
     bw.close(); 

     System.out.println("Done"); 

    } 
} 
+1

你可以提供一个[MCVE为了证明你的问题? – Gendarme

+1

尝试冲洗流? 'bw.flush()'。另外,将构造函数抛出一个异常try {} catch {},以便我们可以看到发生了什么。 –

回答

1

您需要从RAM数据flushHDD|SSD这样:

try (FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw)) { 
     // Write the data to the memory 
     bw.write(content); 
     // You need to flush the data 
     bw.flush(); 
     // Close the BufferedWriter 
     bw.close(); 
} catch (Exception ex) { 
     Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to write the data on the file", ex); 
} 
+1

哦,这是冲洗流,我没有发布,因为我不知道,但我很高兴你做到了。 –

+0

@ Ashwin Gupta;)。 。 。 – GOXR3PLUS

相关问题