Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java text file read & write Code ....

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java text file read & write Code ....

    Help Me to complete My code .... Java txt file read,write code....(Plz Help)

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java text file read & write Code ....

    What have you tried? Post what you currently have.

    Be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    25
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Java text file read & write Code ....

    Java IO FileReader reads files as characters

    import java.io.*;
    public class TestFileReader {
        public static void main(String[] args) {
            Reader reader = null;
            try {
                //Established a connection to the file english.txt
                reader = new FileReader(new File("C:/Users/tim/Desktop/english.txt"));
                // Each time a character is read, the return value is the integer type value of the ACSII.
                //if there is still a character in the file, continue reading until read the end of the file. Return -1
                int l;
                while ((l = reader.read()) != -1) {
                    System.out.println((char) l);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    en.verejava.com/?id=19716081982837

    --- Update ---

    Java IO FileWriter writes characters

    import java.io.*;
     
    public class TestFileWriter {
        public static void main(String[] args) {
            Writer writer = null;
            try {
                writer = new FileWriter(new File("C:/Users/tim/Desktop/english.txt")); //Directory must exist
                //Write data to a file
                String content = "At this moment, you will dream";
     
                writer.write(content);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
     
    }

    en.verejava.com/?id=19716129221838

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Java text file read & write Code ....

    Please don't spoon feed solutions: http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. write & read/load to/from file
    By klskl in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 26th, 2013, 10:39 PM
  2. read & write object in object file
    By amirsarem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2013, 11:35 AM
  3. Read and write to a text file and calculate values
    By s.sariyan in forum What's Wrong With My Code?
    Replies: 19
    Last Post: November 28th, 2012, 10:19 AM
  4. Replies: 0
    Last Post: October 29th, 2011, 02:37 AM
  5. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM