Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Editing TXT file?

    Have you decided how you are going to create a new file with the changed contents? There is more than one way to do it.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Editing TXT file?

    Yes , there are more than one way to do it.

    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    
    public class splitArray{
     public static void main(String[] args) throws IOException {
      BufferedReader inputStream=null;
      PrintWriter outputStream=null;
    
      try{
          inputStream= new BufferedReader(new FileReader("fsm.txt"));
          outputStream = new PrintWriter(new FileWriter("output.txt"));
          String l;
                while ((l = inputStream.readLine()) != null) {
                  if (l.indexOf("gold") != -1)
                    outputStream.println(l.replaceAll("\\d+","12")); 
                   else  outputStream.println(l);
                   
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }
    }
    }

    Ksharp

  3. #23
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    Have you decided how you are going to create a new file with the changed contents? There is more than one way to do it.
    Well in my mind is this way: Insert the whole text in to the string variable, change the string, delete the file, create new and paste the string in to that new file
    I just need file's name to be exactly the same because I am doing this to make an online game

  4. #24
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Ksharp View Post
    Yes , there are more than one way to do it.

    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    
    public class splitArray{
     public static void main(String[] args) throws IOException {
      BufferedReader inputStream=null;
      PrintWriter outputStream=null;
    
      try{
          inputStream= new BufferedReader(new FileReader("fsm.txt"));
          outputStream = new PrintWriter(new FileWriter("output.txt"));
          String l;
                while ((l = inputStream.readLine()) != null) {
                  if (l.indexOf("gold") != -1)
                    outputStream.println(l.replaceAll("\\d+","12")); 
                   else  outputStream.println(l);
                   
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }
    }
    }

    Ksharp
    That's cool but I quite aware of using Bufferreaders/writers

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Editing TXT file?

    Insert the whole text in to the string variable
    If you read all of the text from the file into one String that could make it harder to make changes to one of the lines buried in the String. Why not read the lines and save them one by one in a collection like an arraylist?
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    If you read all of the text from the file into one String that could make it harder to make changes to one of the lines buried in the String. Why not read the lines and save them one by one in a collection like an arraylist?
    Oh alright...
    How do I insert a line in to the file again?

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Editing TXT file?

    Why are you trying to insert a line into an existing file? By "insert" I assume that you mean you want to add a new line to an existing file. Say the file has 2 lines and you want to insert a new line so that the file now has 3 lines. That can be very hard to do.
    It would be better to read the whole file into a collection in memory, make the changes to the line(s) in the collection and write the whole file from the collection instead of trying to insert a line into a file.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    Why are you trying to insert a line into an existing file? By "insert" I assume that you mean you want to add a new line to an existing file. Say the file has 2 lines and you want to insert a new line so that the file now has 3 lines. That can be very hard to do.
    It would be better to read the whole file into a collection in memory, make the changes to the line(s) in the collection and write the whole file from the collection instead of trying to insert a line into a file.
    Oh ok thanks

  9. #29
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Ok there is another question, there are "next();/nextLine(); and so on" methods, it's basically selects the next words/lines but how can I start selection from the beginning?
    Do I have yo create another scanner?

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Editing TXT file?

    Can you Explain what you are trying to do?
    Creating a Scanner object with a file will start the reading of the lines of the file at the first line.
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    nvm I done the code I did: apply all the lines to the array and took the array's number that I need edited it and now I need a while loop that saves/creates/replaces file with that data not sure how to do that

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Editing TXT file?

    An earlier post showed code you could use to write to a file. You said:
    I quite aware of using Bufferreaders/writers
    Can you explain what your problem is?

    Do you have the lines you want to write to the file in an array(list)?
    Do you know how to get the lines out of the array(list)?
    If you don't understand my response, don't ignore it, ask a question.

  13. #33
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    An earlier post showed code you could use to write to a file. You said:

    Can you explain what your problem is?

    Do you have the lines you want to write to the file in an array(list)?
    Do you know how to get the lines out of the array(list)?
    I don't know it's just bufferwriter is quite big code

    Yes my array(list) contains strings each list like:
    array[0] = "iron 5"
    array[1] = "gold 45'

    To get them in to the file I wanna use loop
    maxList = 1;
    while(list <maxList){
    writefile.writeline(array(list), "user.txt");//I made it up
    list++
    }
    Something like that, I am not sure what do I replace writefile.writeline with...
    WARNING I am Russian so it's possible that I wont understand you correctly...

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Editing TXT file?

    Did you try the code in post#22? You could use parts of that for your program.
    If you don't understand my response, don't ignore it, ask a question.

  15. #35
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Oh ok, now everything working perfectly thanks!
    WARNING I am Russian so it's possible that I wont understand you correctly...

  16. #36
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Ksharp View Post
    Yes , there are more than one way to do it.

    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    
    public class splitArray{
     public static void main(String[] args) throws IOException {
      BufferedReader inputStream=null;
      PrintWriter outputStream=null;
    
      try{
          inputStream= new BufferedReader(new FileReader("fsm.txt"));
          outputStream = new PrintWriter(new FileWriter("output.txt"));
          String l;
                while ((l = inputStream.readLine()) != null) {
                  if (l.indexOf("gold") != -1)
                    outputStream.println(l.replaceAll("\\d+","12")); 
                   else  outputStream.println(l);
                   
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }
    }
    }

    Ksharp
    nvm what I said, thanks for providing me the PrintWriter example!
    WARNING I am Russian so it's possible that I wont understand you correctly...

  17. #37
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Topic completed, close it please :D
    WARNING I am Russian so it's possible that I wont understand you correctly...

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Simple Text File Editing in Java help
    By DMarsh12 in forum New To Java
    Replies: 1
    Last Post: 10-22-2011, 09:55 PM
  2. editing a file. Am I doing this right?
    By Mokomi in forum New To Java
    Replies: 2
    Last Post: 10-14-2011, 04:40 AM
  3. sound editing
    By nikkka in forum Advanced Java
    Replies: 3
    Last Post: 04-14-2011, 02:34 PM
  4. FileWriter editing hosts file Mac
    By danborgir in forum New To Java
    Replies: 2
    Last Post: 01-12-2011, 11:23 AM
  5. Photo Editing
    By stekun in forum Advanced Java
    Replies: 3
    Last Post: 02-23-2010, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •