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 12 of 12

Thread: Save array to text file

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Save array to text file

    I have an array [a, b, c, d] that I wish to save to a .txt file. My code allows a user to edit a specific line of text file as it deletes the rest of the files contents on edit I have decided to save the contents of a file into an array. I then want to save the contents of the array back into the text file.

    import java.io.*;
    import java.util.*;
     
    public class LineNumberReaderExample {
        public static void main(String[] args) throws Exception {               
                boolean foundIt = false;
                Scanner input=new Scanner(System.in);
                System.out.print("Enter record to edit ");
                String word=input.next();
                File f=new File("file.txt");
                BufferedReader freader;
                freader = new BufferedReader(new FileReader(f));
                String s;
                List<String> list = new ArrayList<String>();
                while((s = freader.readLine()) != null) {
                       list.add(s);
                       System.out.print(list);
                       if(s.startsWith(word)){
                       while(foundIt == false) {
                       System.out.print(s);
                       foundIt = true;
                       System.out.print("Replace With: ");
                       String newtext=input.next();
                       FileWriter writer = new FileWriter("file1.txt");
                       writer.write(s.replace(s, newtext));
                       writer.close();
                       list.add(newtext);
                    }
                 }               
     
            System.out.println(list);
            // NEED CODE TO SAVE ARRAY TO FILE // 
             } 
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Save array to text file

    The code you posted does not make sense.
    First there is an array of int values.
    Then there is a List that hold Strings.
    It doesn't show where anything is added to the list.
    There is no definition of the variable: Writer.

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save array to text file

    I have now edited my thread to include my whole program

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Save array to text file

    Can you explain what the problem is? What have you tried and what happened?

    Where is the array that you are working with?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save array to text file

    I have a text file that includes [a, b, c, d], when this program is run it adds the elements to an array and allows the user to replace an element.

    For example if a user replaces "d" with "e" the file will be saved to array, so the array looks like [a, b, c, e].

    I'm having problems with saving said array back into the text file

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Save array to text file

    Where is the array you are talking about? I do not see an array in the posted code.

    text file that includes [a, b, c, d]
    Is that the contents of the line that is in the file? Does the file only have one line?
    Is the program supposed to read the one line from the file, change one of the characters and write the changed line to another file?

    I'm having problems with saving said array back into the text file
    Please explain what the problems are.

    BTW The code is poorly formatted. Code inside of {}s should be indented 3-4 spaces to show the nesting levels.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save array to text file

    The text file has several lines,
    a
    b
    c
    d

    The code runs through each line until it finds what it is looking for, what the user entered, whilst it is doing this it adds the lines into an array. After the string is found it then adds the remaining lines to an array.

    So if the user was looking for "c" the system would run as follows:

    Line 1 - a. Array = [a]. C NOT FOUND
    Line 2 - b. Array = [a, b]. C NOT FOUND
    Line 3 - C. Array = [a, b, c]. C FOUND
    ***User replaces "c" with "e"***
    c removed from array. Array = [a, b]
    e added to array. Array = [a, b, e]
    Line 4 - d, Array = [a, b, e, d]

    At this point I want to add the array [a, b, e, d] to a text file and I don't know how to do this.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Save array to text file

    I want to add the array [a, b, e, d] to a text file
    Sorry, I do NOT see any arrays in the code. Where is the array you are talking about?

    Do you mean ArrayList? Are you asking how to go through the elements in an arraylist one by one and write each element to a line in a file?
    First: write a loop that gets the elements from the arrraylist one at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save array to text file

    Sorry yes my ArrayList, if I write a loop that gets each element it will just overwrite the file each time it loops so I will end up with a file that just has the last element in it. Or is there a way to add a new line of text into a text file? I'm new to Java

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Save array to text file

    I think you should start at the beginning before trying to make changes to the posted code. Go through the steps you listed in post#7 and write the code that follows those steps. One change I would make is to test the line if it is the one to be changed BEFORE adding the line to the arraylist. It seems a waste of effort to add a line to the arraylist and then immediately replace it.
    After all the data is in the the arraylist, then write a loop to go through the arraylist and get the elements one at a time.

    write a loop that gets each element it will just overwrite the file each time it loops
    Not if you open the file before the loop and close the file after the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save array to text file

    I've solved it, thank you for the loop sugestion.

    for (int i = 0; i < list.size(); i++)
                    writer.write(list.get(i) + "\r\n");
                    writer.close();

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Save array to text file

    The posted code's indentations is confusing. It looks like the call to the close() method is inside the loop.
    You should always use {}s with loops to make the code clearer to read.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading from a text file into a two dimensional array
    By Hayfield in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: May 13th, 2012, 04:36 PM
  2. How to read a 2 dimensional text file from an array?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 07:44 AM
  3. loading things from a text file into an array list
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2011, 02:32 PM
  4. Text File into String Array
    By mathanv in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 04:30 AM
  5. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 10:56 AM

Tags for this Thread