Writing part of a line to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ioshonowo
    New Member
    • Sep 2007
    • 31

    Writing part of a line to a file

    RandomAccessFil e readName = null;
    RandomAccessFil e nameWriter= null;
    List<String> Names = new ArrayList<Strin g>();
    char[] pan= new char[19];
    char[] cardName= new char[23];
    try{
    readName= new RandomAccessFil e(persoNames + "/perso.txt","rw" );
    nameWriter= new RandomAccessFil e("C:/perso.txt","rw" );
    for (String line = readName.readLi ne(); line != null;
    line = readName.readLi ne()) {
    if (line != null){
    //readName.
    line.getChars(0 ,19,pan,0);
    line.getChars(1 9,42,cardName,0 );
    nameWriter.writ eChars(pan.toSt ring());

    I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

    Could you direct me to any web resources to help me correct the code.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ioshonowo
    RandomAccessFil e readName = null;
    RandomAccessFil e nameWriter= null;
    List<String> Names = new ArrayList<Strin g>();
    char[] pan= new char[19];
    char[] cardName= new char[23];
    try{
    readName= new RandomAccessFil e(persoNames + "/perso.txt","rw" );
    nameWriter= new RandomAccessFil e("C:/perso.txt","rw" );
    for (String line = readName.readLi ne(); line != null;
    line = readName.readLi ne()) {
    if (line != null){
    //readName.
    line.getChars(0 ,19,pan,0);
    line.getChars(1 9,42,cardName,0 );
    nameWriter.writ eChars(pan.toSt ring());

    I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

    Could you direct me to any web resources to help me correct the code.
    You want to insert text at a particular position in a file or append it to the end?
    Why are you using two RandomAccessFil e objects?

    Comment

    • ioshonowo
      New Member
      • Sep 2007
      • 31

      #3
      Originally posted by r035198x
      You want to insert text at a particular position in a file or append it to the end?
      Why are you using two RandomAccessFil e objects?

      /*
      * NamesCount.java
      *
      * Created on 13 September 2007, 14:06
      *
      * To change this template, choose Tools | Template Manager
      * and open the template in the editor.
      */

      package namescount;
      import java.io.*;
      import java.util.*;
      /**
      *
      * @author oshonowo
      */
      public class NamesCount {
      public static void main(String[] args) throws Exception{
      File Dir = new File("//10.10.0.127/c$/Documents and Settings/oshonowo/My Documents/Chams to be burnt/20070921");
      List<String> allNames = new ArrayList<Strin g>();
      if( Dir.exists() && Dir.isDirectory ()){
      String[] location = Dir.list();
      for(int i=0; i <location.lengt h; i++){
      File dirs= new File(Dir + "/" + location[i]);
      //SumOfCards+=Num berofLines(dirs );
      allNames.add(Pr intNames(dirs)) ;
      }
      }
      System.out.prin tln(" Names are "+ allNames);
      //System.out.prin tln("Sum of cards is "+ SumOfCards);

      }

      static String PrintNames(File persoNames) throws Exception{
      RandomAccessFil e readName = null;
      RandomAccessFil e nameWriter= null;
      List<String> Names = new ArrayList<Strin g>();
      char[] pan= new char[19];
      char[] cardName= new char[23];
      try{
      readName= new RandomAccessFil e(persoNames + "/perso.txt","rw" );
      nameWriter= new RandomAccessFil e("C:/perso.txt","rw" );
      for (String line = readName.readLi ne(); line != null;
      line = readName.readLi ne()) {
      if (line != null){
      //readName.
      line.getChars(0 ,19,pan,0);
      line.getChars(1 9,42,cardName,0 );
      nameWriter.writ eChars(pan.toSt ring());
      //for (int i=0; i< pan.length; i++){ nameWriter.writ eChars(pan.);}
      //for (int i=0; i< cardName.length ; i++){nameWriter .writeBytes(car dName[i]);}
      //System.out.prin tln(pan + " " + cardName);
      Names.add(line) ;
      nameWriter.writ eBytes(System.g etProperty("lin e.separator"));
      }
      }
      //Names.add("%n") ;
      if (Names.isEmpty( )!= true){
      Collections.sor t(Names);
      //System.out.prin tln(Names);
      nameWriter.writ eBytes(Names.to String());
      }
      }catch (Exception e) {
      System.err.form at("Could not read %s: %s%n", Names, e);
      System.exit(1);
      } finally {
      if (readName != null) {
      try {
      readName.close( );
      } catch (IOException e) {}
      }
      if (nameWriter != null) {
      try {
      nameWriter.clos e();
      } catch (IOException e) {}
      }
      }
      return Names.toString( );

      }

      /* static int NumberofLines(F ile file) throws IOException{
      BufferedReader reader = null;
      int lineCount = 0;
      try {
      reader = new BufferedReader( new FileReader(file + "/perso.txt"));
      for (String line = reader.readLine (); line != null;
      line = reader.readLine ()) {
      lineCount++;
      }
      } catch (IOException e) {
      System.err.form at("Could not read %s: %s%n", file, e);
      System.exit(1);
      } finally {
      if (reader != null) {
      try {
      reader.close();
      } catch (IOException e) {}
      }
      }
      System.out.prin tln("Number of Lines are " + lineCount);
      return lineCount;
      }*/
      }

      I am trying to write a program that will read the contents of a directory on another system and prints the relevant parts to a file.

      I just need to know where I can find info on how to read part of a string representing a line of info., extract the relevant parts and write them to a different file.

      I have tried using the API on the sun website but it is a bit hard for a beginner like me.

      This is office work. Pls help.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by ioshonowo
        [code=java]
        RandomAccessFil e readName = null;
        RandomAccessFil e nameWriter= null;
        List<String> Names = new ArrayList<Strin g>();
        char[] pan= new char[19];
        char[] cardName= new char[23];
        try{
        readName= new RandomAccessFil e(persoNames + "/perso.txt","rw" );
        nameWriter= new RandomAccessFil e("C:/perso.txt","rw" );
        for (String line = readName.readLi ne(); line != null;
        line = readName.readLi ne()) {
        if (line != null){
        //readName.
        line.getChars(0 ,19,pan,0);
        line.getChars(1 9,42,cardName,0 );
        nameWriter.writ eChars(pan.toSt ring());
        [/code]

        I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

        Could you direct me to any web resources to help me correct the code.
        Use Code Tags while you do Post!
        Now try to extract the line.length(), whether it is 42 bytes long or something else :-)

        Kind regards,
        Dmjpro.

        Comment

        • ioshonowo
          New Member
          • Sep 2007
          • 31

          #5
          Originally posted by dmjpro
          Use Code Tags while you do Post!
          Now try to extract the line.length(), whether it is 42 bytes long or something else :-)

          Kind regards,
          Dmjpro.


          It is something else. It changes for every line.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by ioshonowo
            It is something else. It changes for every line.
            Now explain me two lines below. :-)

            [code=java]
            line.getChars(0 ,19,pan,0);
            //Here you sure that first 19 bytes will be Pan
            line.getChars(1 9,42,cardName,0 );
            //Here you same too.....
            [/code]

            I assumed that first 19 bytes will be Pan and then rest bytes will be Card.

            Now try with this modified line..
            [code=java]
            line.getChars(1 9,line.length() ,cardName,0);
            [/code]

            Best of Luck with your Try!

            Kind regards,
            Dmjpro.

            Comment

            • ioshonowo
              New Member
              • Sep 2007
              • 31

              #7
              [QUOTE=dmjpro]Now explain me two lines below. :-)

              [code=java]
              line.getChars(0 ,19,pan,0);
              //Here you sure that first 19 bytes will be Pan
              line.getChars(1 9,42,cardName,0 );
              //Here you same too.....
              [/code]

              I assumed that first 19 bytes will be Pan and then rest bytes will be Card.

              Now try with this modified line..
              [code=java]
              line.getChars(1 9,line.length() ,cardName,0);
              [/code]

              Best of Luck with your Try!

              Kind regards,
              Dmjpro.[/QUOTE

              I get the characters I need from the string but the char[].toString() print jargon in the file. See below
              [code=java] nameWriter.writ eChars(pan.toSt ring());[/code]

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Print it separately.
                pan.toString()
                See what it prints.
                What you tried to do, do it using substring.
                Have a look at this.

                [code=java]
                String pan = null, cardName = null;
                //line.getChars(0 ,19,pan,0);
                pan = line.substring( 0,19);
                //line.getChars(1 9,42,cardName,0 );
                cardName = line.substring( 19);
                //nameWriter.writ eChars(pan.toSt ring());
                nameWriter.writ eChars(pan);
                [/code]

                Good Luck :-)

                Kind regards,
                Dmjpro.

                Comment

                • ioshonowo
                  New Member
                  • Sep 2007
                  • 31

                  #9
                  Originally posted by dmjpro
                  Print it separately.
                  pan.toString()
                  See what it prints.
                  What you tried to do, do it using substring.
                  Have a look at this.

                  [code=java]
                  String pan = null, cardName = null;
                  //line.getChars(0 ,19,pan,0);
                  pan = line.substring( 0,19);
                  //line.getChars(1 9,42,cardName,0 );
                  cardName = line.substring( 19);
                  //nameWriter.writ eChars(pan.toSt ring());
                  nameWriter.writ eChars(pan);
                  [/code]

                  Good Luck :-)

                  Kind regards,
                  Dmjpro.

                  Thanks it works but I have spaces in between the characters. How do I get rid of them?

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by ioshonowo
                    Thanks it works but I have spaces in between the characters. How do I get rid of them?
                    Nice to see that you got your code worked.
                    I think you better to write a method which will do that.
                    Start writing Code then we are here to help you.
                    Please post that problem in separate if you need help.
                    Good Luck!

                    Kind regards,
                    Dmjpro.

                    Comment

                    • ioshonowo
                      New Member
                      • Sep 2007
                      • 31

                      #11
                      Hello. I am trying to write to a file from a file. The problem is the position of the characters I need to write are not always in the same for each line of data.
                      However, the number of preceeding commas before the characters is consistent.

                      Any suggestions on how to perform this task?

                      Comment

                      Working...