我正在将字符串的长度写入文件中,结果是该值被视为 ASCII 值,并且用 ASCII 值指定的字符写入文件,然后我尝试在 FileInputStream 的帮助下读取该字符,然后BufferInputStream 并且结果不会显示在控制台上。为什么文件中的字符没有打印到控制台?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条经验 获得超13个赞
首先查看问题是否出在您写入文件的方式中 请
遵循此链接 如何在 Java 中读取文件 并查看此链接如何在 Java 中写入文件 问题不在 Asci 因为如果文件的长度字符串是 int (应该是)在将其写入文件之前,您应该使用 Integer.tostring(length_of_the_string) 对其进行解析
而不是这条线
fout.write(length);
写这一行
fout.write(Integer.toString(length));
您可以在询问 Stackoverflow 之前 Google 解决您的问题(它可能会在 Stackoverflow 之前解决)
添加回答
举报
0/150
提交
取消