WHERE would the txt file be located? How would you set it if you wanted to create a txt file, write to it, and place it in like...your instructors computer for evaluation? : )
Hello there, This code does not really work for me. Here's how I tetesd it. My test.txt file is saved with UTF-8 encoding and contains this line: w e9te9 jedn? stron? f4pe8e7 dbg fcte0 My test program below first reads the file in BufferedReader and then writes in Writer. package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer;public class Test_temp { public Test_temp() { String sPath = E:/workspace/project/src/test/test.txt ; if (sPath != null && !sPath.trim().equals( )) { try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sPath + .new ), UTF8 )); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(sPath), UTF8 )); String s = null; while ((s = in.readLine()) != null) { String UTF8Str = new String(s.getBytes(), UTF8 ); System.out.println( [" + UTF8Str + "] ); out.append(UTF8Str).append( \r\n ); } System.out.println( Reading Process Completly Successfully. ); in.close(); out.flush(); out.close(); } catch (UnsupportedEncodingException ue) { System.out.println( Not supported : + ue.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } } public static void main(String[] args) { new Test_temp(); } } The new generated file (test.txt.new) is also encoded with UTF-8 but characters are corrupted: ?w ?t? jedn? stron? ?p?? ?g ? Could you please tell me what do I do wrong? Thanks
programimg md.rahid islam July 30, 2012 at 9:46 PM
fileWriter locationacl May 24, 2011 at 7:32 AM
WHERE would the txt file be located? How would you set it if you wanted to create a txt file, write to it, and place it in like...your instructors computer for evaluation? : )
Nice!Misha May 28, 2011 at 4:30 AM
How I able to save the file in the path that I choose?
hi :)jeryl September 20, 2011 at 5:41 PM
this really helped me with my project... the right source :) thanks
hi :)jeryl September 20, 2011 at 5:42 PM
this really helped me with my project... the right source :) thanks
Regarding post.Arvind K December 17, 2011 at 12:50 PM
The post was useful...
Works!Apoorva February 19, 2012 at 12:25 AM
Thanks for this post! it's easy to understand!
jyfjyhgfh February 27, 2012 at 7:59 PM
import java.io.*; import java.lang.*; import java.util.*; public class Cf { private Formatter x; public void openFile() { try { x = new Formatter("C:/Users/travikanth/Workspaces/MyEclipse 8.x/practice/src/po.txt"); } catch(Exception e) { System.out.println("error"+ e.toString()); } } public void addRecords() { x.format("%s,%s,%s", "2","ravi","23"); } public void closeFile() { x.close(); } public static void main(String[] args) { Cf a = new Cf(); a.openFile(); a.addRecords(); a.closeFile(); } }
I get an errorkiboigo April 3, 2012 at 11:40 PM
When i try to complie i get the error below Could some help "Error: The public type WriteTextFileExample must be defined in its own file"
gAfCGlnzmsghtAntonio July 23, 2012 at 7:07 AM
Hello there, This code does not really work for me. Here's how I tetesd it. My test.txt file is saved with UTF-8 encoding and contains this line: w e9te9 jedn? stron? f4pe8e7 dbg fcte0 My test program below first reads the file in BufferedReader and then writes in Writer. package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer;public class Test_temp { public Test_temp() { String sPath = E:/workspace/project/src/test/test.txt ; if (sPath != null && !sPath.trim().equals( )) { try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sPath + .new ), UTF8 )); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(sPath), UTF8 )); String s = null; while ((s = in.readLine()) != null) { String UTF8Str = new String(s.getBytes(), UTF8 ); System.out.println( [" + UTF8Str + "] ); out.append(UTF8Str).append( \r\n ); } System.out.println( Reading Process Completly Successfully. ); in.close(); out.flush(); out.close(); } catch (UnsupportedEncodingException ue) { System.out.println( Not supported : + ue.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } } public static void main(String[] args) { new Test_temp(); } } The new generated file (test.txt.new) is also encoded with UTF-8 but characters are corrupted: ?w ?t? jedn? stron? ?p?? ?g ? Could you please tell me what do I do wrong? Thanks
programimg md.rahid islam July 30, 2012 at 9:46 PM
how to making ajava programimg file
as400venkataramana September 14, 2012 at 9:24 PM
how to write java program in ile
JAVA Kym September 19, 2012 at 7:43 AM
can you give some more complex examples
questionyhen October 3, 2012 at 11:12 PM
where will the file be saved?
Post your Comment