2017-05-08 36 views
0

我需要寫一個文件寫的,但我一定要把這件以這樣的方式如何在爲InputStream

InputStream in = this.getClass().getResourceAsStream("/Grafica/indice_corrente.txt"); 

現在我需要在這寫的InputStream值10,但我不知道如何我可以做這個。任何人都可以幫助我?

+4

你不能。這是一個輸入流。你只能從中讀取。 – f1sh

+0

此外,您無法寫入應用程序資源。如果您想更改indice_corrente.txt,請使用該名稱創建一個*新文件*,該文件位於用戶主目錄下的某個臨時文件或文件中,然後寫入該文件。 – VGR

回答

2

你不能在InputStream中寫入。如果你想寫入文件,你需要一個的FileWriter的BufferedWriterFileOutputStream中

0

您無法寫入InputStream。

要寫入值10,你可以做這樣的事情:

要寫入ASCII中的10個(這意味着你會寫字符1和0)。你可以這樣做。

PrintWriter out = new PrintWriter("/Grafica/indice_corrente.txt"); 
out.print(10); 

如果你想要寫字節10,用這個

file = new File("/Grafica/indice_corrente.txt"); 
os = new FileOutputStream(file); 
os.write(10); 

確實注意到然而,這將覆蓋在這個文本文件中的一切。如果你想追加,而不是覆蓋,使用這個:

file = new File("/Grafica/indice_corrente.txt"); 
os = new FileOutputStream(new FileWriter(file, true)); 
os.write(10); 
相關問題