Results 1 to 8 of 8
  1. #1
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default RandomAccessFile Question

    Hey guys!
    Could you please have a short look on my code and tell me why it does not work how I wish!?
    This is a program that is supposed to read it's own code to store it in a String[] array "content".
    It adds line-numbers before each line.
    It seeks the main-method to add a comment in the line before it.
    It should write the array linewise in a text file. This does not work, because it does not start a new line!
    How can I write the array content line by line to a text file?
    Thanks for helping!

    Java Code:
    import java.io.*;
    public class RandomAccessFileDemo {
      
      public static void main(String[] args) {
        String[] content = new String[100];
        try {
          
          RandomAccessFile quell = new RandomAccessFile("RandomAccessFileDemo.java", "r");
          RandomAccessFile ziel = new RandomAccessFile("RandomAccessFileDemo.txt", "rw");
          
          int counter = 0;
          
          long seek = 0;
          long length = quell.length();
          
          while(seek < length) { 
            String s = quell.readLine();
            content[counter] = counter+" "+s;
            seek = quell.getFilePointer();
            counter++;
          }  
          quell.close();
          
          System.out.println("Number of lines: "+counter);
          
          for(int i=0; i<counter;i++){
            //Checks if the line contains the main() method while avoiding the check-line
            if (content[i].contains("main(String[] args")&!content[i].contains("contains")){
              System.out.println("\n Comment \n");          
            }
            //show's the content just for testing purposes
             System.out.println(content[i]);
    
            //writes the content to the textfile: "\n" should start a new line which doesn't work
            ziel.writeBytes(content[i]+"\n");
          }
          ziel.close();
    
        } catch (IOException e)
        {
          e.printStackTrace();
        }
        
      }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: RandomAccessFile Question

    I really don't understand why you're using RandomAccessFiles for this; all you do it read lines sequentially and write them sequentially after processing them.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Re: RandomAccessFile Question

    Quote Originally Posted by JosAH View Post
    I really don't understand why you're using RandomAccessFiles for this; all you do it read lines sequentially and write them sequentially after processing them.

    kind regards,

    Jos
    I've got another program which does a similar job. This one reads it's own code and copys it in a text file. fw.write(s.replaceAll("i", "w")+ "\n", 0, arrayLaenge); replaces letters w for i :) This one works pretty well, in the text file, the program is as in the editor...
    Java Code:
    package inpOutBeisp;
    import java.io.*;
    public class FileCopyCharChange{
      public static void main(String[] args){
        File dir = new File(".\\inpOutBeisp");
        File fil = new File(dir, "FileCopyCharChange.java");
        File copy = new File("FileCopyCharChange.txt");
        int i;
        FileReader fr;
        FileWriter fw;
        
        char[] temp = new char[1000];
        try{
          fr = new FileReader(fil);
          fw = new FileWriter(copy);
          int arrayLaenge = fr.read(temp);
          fr.close();
          String s = new String(temp, 0, arrayLaenge);
          fw.write(s.replaceAll("i", "w")+ "\n", 0, arrayLaenge);
          //fw.write(s, 0, arrayLaenge);
          fw.write("/* Kommentar am Ende der Datei*/");
          fw.close();
        }
        catch(FileNotFoundException ffe){
          System.out.println("Datei nicht gefunden");
        }
        catch(Exception e){}
      }
    }
    My program from my first post with RandomAccessFile does not do this. When I copy the text file on the board here, lines are numbered and after each line a new one begins. Just like I wanted. But in the text file it's all in two lines. Why? I don't understand why this does not work!
    In the program from my first post:
    Java Code:
    ziel.writeBytes(content[i]+"\n");
    should write the array linewise to the textfile!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: RandomAccessFile Question

    On a MS Windows machine you should terminate a line with the "\r\n" character pair. There's a "line.separator" property in the System class that contains the correct sequence for your system. It you had used an ordinary PrintStream of a PrintWriter you needn't have to deal with it ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Re: RandomAccessFile Question

    Many thanks man! This is my homework then:
    Code that copys itself to a text file using RandomAccessFile. It numbers the lines and adds a comment in the line before main :)

    Java Code:
    import java.io.*;
    
    public class RandomAccessFileDemo {
      
      public static void main(String[] args) {
        String[] content = new String[100];
        try {
          RandomAccessFile quell = new RandomAccessFile("RandomAccessFileDemo.java", "r");
          RandomAccessFile ziel = new RandomAccessFile("RandomAccessFileDemo.txt", "rw");
          
          int counter = 0;
          
          long seek = 0;
          long length = quell.length();
          
          while(seek < length) { 
            String s = quell.readLine();
            content[counter] = s;
            seek = quell.getFilePointer();
            counter++;
          }  
          quell.close();
          System.out.println("Number of lines: "+counter);
          int lineNumber = 1;
          for(int i=0; i<counter;i++){
            //Checks if the line contains the main() method while avoiding the check-line
            if (content[i].contains("main(String[] args")&!content[i].contains("contains")){
              ziel.writeBytes(lineNumber+" "+" Kommentar "+"\r\n");
              lineNumber = lineNumber+1;          
            }
            ziel.writeBytes(lineNumber+" "+content[i]+"\r\n");
            lineNumber = lineNumber+1;
          }
          ziel.close();
        } catch (IOException e)
        { e.printStackTrace(); }
      }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: RandomAccessFile Question

    Note that your solution only works on an MS Windows machine, Unix and Linux boxes and Macs get an extra \r or \n character after each line; have a look at my previous suggestion.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    Mandingo is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Re: RandomAccessFile Question

    Great!!! PrintWriter writes line by line!
    I wondered why it didn't worked but I hadn't changed the file names to read from/write to, so it still used the old code to write in the file :)
    Guess it's already late for my only left lonely brain cell =p

    Java Code:
    import java.io.*;
    
    public class RandomAccessFilePrintWriter {
      
      public static void main(String[] args) {
        String[] content = new String[100];
        try {
          RandomAccessFile quell = new RandomAccessFile("RandomAccessFilePrintWriter.java", "r");
          PrintWriter ziel = new PrintWriter(new FileWriter("RandomAccessFilePrintWriter.txt"));
          
          int counter = 0;
          
          long seek = 0;
          long length = quell.length();
          
          while(seek < length) { 
            String s = quell.readLine();
            content[counter] = s;
            //content[counter] = counter+" "+s;
            seek = quell.getFilePointer();
            counter++;
          }  
          quell.close();
          System.out.println("Number of lines (ohne Kommentar): "+counter);
          int lineNumber = 1;
          for(int i=0; i<counter;i++){
            //Checks if the line contains the main() method while avoiding the check-line
            if (content[i].contains("main(String[] args")&!content[i].contains("contains")){
              ziel.println(lineNumber+" "+" Kommentar ");
              lineNumber = lineNumber+1;          
            }
            ziel.println(lineNumber+" "+content[i]);
            lineNumber = lineNumber+1;
          }
          ziel.flush();
          System.out.println("done");
        } catch (IOException e)
        { e.printStackTrace(); }
      }
    }
    Is there a way to make this one more nicely:
    String[] content = new String[100];
    How can I find out the length of the array to write in?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: RandomAccessFile Question

    Quote Originally Posted by Mandingo View Post
    Is there a way to make this one more nicely:
    String[] content = new String[100];
    How can I find out the length of the array to write in?
    Don't use an array; arrays are so previous century; use a List<String> instead.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Writing the data into RandomAccessFile
    By vasavi.singh in forum New To Java
    Replies: 5
    Last Post: 01-31-2013, 08:12 AM
  2. Question about RandomAccessFile and MappedByteBuffer
    By Pojahn_M in forum New To Java
    Replies: 17
    Last Post: 06-23-2011, 01:13 PM
  3. read from RandomAccessFile
    By peliukasss in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 09:34 AM
  4. RandomAccessFile
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 04:05 PM

Posting Permissions

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