我正在將字符串的長度寫入文件中,結(jié)果是該值被視為 ASCII 值,并且用 ASCII 值指定的字符寫入文件,然后我嘗試在 FileInputStream 的幫助下讀取該字符,然后BufferInputStream 并且結(jié)果不會顯示在控制臺上。為什么文件中的字符沒有打印到控制臺?import java.io.*;class Fileoutput{ public static void main(String args[]) { try { File f=new File("C:\\Users\\parsh\\YG2108\\Testfile.txt"); FileInputStream fins=new FileInputStream("C:\\Users\\parsh\\YG2108\\Testfile.txt"); FileOutputStream fouts=new FileOutputStream("C:\\Users\\parsh\\YG2108\\Testfile.txt"); FileWriter bf=new FileWriter("C:\\Users\\parsh\\YG2108\\Testfile.txt"); BufferedInputStream fin=new BufferedInputStream(fins); BufferedOutputStream fout=new BufferedOutputStream(fouts); BufferedWriter bw=new BufferedWriter(bf); int i; String s1="Good Afternoon have a nice day frghunv9uhbzsmk zvidzknmbnuf ofbdbmkxm;jccipx nc xdibnbnokcm knui9xkbmkl bv"; int length=s1.length(); //findin string length System.out.println(length); //printing length on console fout.write(length); //writting into the file System.out.println("Sucess"); while((i=fin.read())!=-1) { System.out.print((char)i); } bw.close(); fout.close(); fin.close(); bf.close(); fouts.close(); fins.close(); System.out.println("All done"); }catch(Exception e){System.out.println(e);} }}
2 回答

蕭十郎
TA貢獻1815條經(jīng)驗 獲得超13個贊
首先查看問題是否出在您寫入文件的方式中 請
遵循此鏈接 如何在 Java 中讀取文件 并查看此鏈接如何在 Java 中寫入文件 問題不在 Asci 因為如果文件的長度字符串是 int (應(yīng)該是)在將其寫入文件之前,您應(yīng)該使用 Integer.tostring(length_of_the_string) 對其進行解析
而不是這條線
fout.write(length);
寫這一行
fout.write(Integer.toString(length));
您可以在詢問 Stackoverflow 之前 Google 解決您的問題(它可能會在 Stackoverflow 之前解決)
添加回答
舉報
0/150
提交
取消