hello friends, making program in txt file being read , shown output. using filereader , editor of eclipse juno this. while doing this, able read full txt file not first character. example, have txt file in which: "freedom of spartacus" written, compiler should have show in result whole string. instead of this, showing "reedom of spartacus" not showing first character. here code:
package file; import java.io.file; import java.io.filereader; import java.io.ioexception; public class o { public static void main(string[] args) throws ioexception { file f1=new file ("tj.txt"); filereader f2=new filereader(f1); f2.read(); system.out.println("starting read"); long size=f1.length(); char[] x=new char[(int)size]; f2.read(x); f2.close(); string s1=new string(x); system.out.println(s1); } } what wrong code , can 1 me this?
you reading first character , ignoring here
f2.read(); remove line , work want.
the length length in bytes, not characters. may same in case, should read byte[] , use build string.
instead suggest
fileinputstream fis = new fileinputstream("tj.txt"); byte[] bytes = new bytes[(int) fis.getchannel().size()]; fis.read(bytes); fis.close(); string s = new string(bytes, "utf-8"); // or preferred encoding.
Comments
Post a Comment