Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By bnson

Thread: Java- Writing a file and reading a file line by line

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Java- Writing a file and reading a file line by line

    1.What is the default place/location ( where the .txt file written is saved) for file write in java, using class FileWriter and BufferedWriter. Is it saved in the package in which lies the class which contains the code for file write?

    2.Can we save the .txt file created after file writing using java class FileWriter and BufferedWriter?

    3.When we want to read a file line by line using class DataInputStream, where should the .txt file be located?

    4.Can we read a .txt file located at a place of our choice (e.g Desktop)?


    Friends please reply as soon as possible because I need a solution to this before I can proceed with my work.
    I shall be grateful!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Do you have a program that you are trying to read and write files in?

    If you write a simple test program and execute it, you can get the answers to all your questions.

  3. #3
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    1.What is the default place/location ( where the .txt file written is saved) for file write in java, using class FileWriter and BufferedWriter. Is it saved in the package in which lies the class which contains the code for file write?
    You can setting default place/location of file *.txt saved, or if you created like code following:
    Java Code:
    File file_1 = new File("default.txt");
    Almost IDEAS of java will default save to folder Project for you.(at least for Netbeans ^^!).

    Can we save the .txt file created after file writing using java class FileWriter and BufferedWriter?
    After using FileWrite and BufferedWriter you has create a file, but you have created other function like as "Save as...."

    3.When we want to read a file line by line using class DataInputStream, where should the .txt file be located?
    When created a DataInputStream you must declare the location of the file.
    Java Code:
    DataInputStream input = new DataInputStream(new FileInputStream("C:\\binary.txt"));
    4.Can we read a *.txt file located at a place of our choice (e.g Desktop)?
    Yes, you can, and it is mandatory, to read you have file.

    You can reference Basic IO.
    And you reference my exercises ago: JNote but I'm only used FileWriter and FileReader ^^!

    Have you nice day,...

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    If you have read any tutorials on IO you would have known these things. I think that you are either jumping in over your head, or trying to pass off a homework assignment on us. I'm with Norm, do you have any code that you're trying this with but can't get to work?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    If you have read any tutorials on IO you would have known these things. I think that you are either jumping in over your head, or trying to pass off a homework assignment on us. I'm with Norm, do you have any code that you're trying this with but can't get to work?
    I agree that I am really week at java, n this is what I am trying to do: to learn it and clearify my concepts.
    This is the class which contains code for file read and write.

    Java Code:
    //BB002260>aggggggatgggggccccccgcgcctccgcgcgcgcgctcgctgcggcgtcgcgcgcgcccccccaaaaaatgagcgcatccgggcgggcggcggtct/////////////
    
    
    
    package dnaclassfinal;
    
    import java.io.*;
    
    import javax.swing.*;
    
    
    public class DNA {
        String name;
        String seq;
    
        DNA(){}
    
        DNA(String name, String seq){
            this.name=name;
            this.seq=seq;
        }
    
        String getName(){
        return JOptionPane.showInputDialog(this,"Name of the sequence:");
       }
    
        String getSeq(){
        return JOptionPane.showInputDialog(this,"Sequence:");
    
        }
    
    
        String displayInFasta(){
            String b=new String();
            b= name.concat(">");
            return b.concat(seq);
        }
    
    
        void writeToFile(String h){
        try
        {
           FileWriter fw = new FileWriter ( "FastaDNA.txt" );
           BufferedWriter  bw = new BufferedWriter ( fw );
    
    
           String stringFile =h;
           bw.write(stringFile);
           bw.close( );
        }
            catch(Exception e) {
          System.out.println("Exception: " + e);
        }
        }
    
    
    
        void readFromFile(){
           String stringFromFile= new String();
    
            try
           {
               FileReader fro = new FileReader( "G:\\FastaDNA.txt" );
               BufferedReader bro = new BufferedReader( fro );
              String strline= bro.readLine( );
    
              stringFromFile= "";
              while( strline!= null )
              {  stringFromFile+= strline;
              strline= bro.readLine( );
              }
              System.out.println(stringFromFile);
              bro.close( );}
           catch( FileNotFoundException filenotfoundexxption)
           {
               System.out.println( "FastaDNA.txt, does not exist" );
           }
           catch( IOException ioexception)
           {
               ioexception.printStackTrace( );
           }
    
    
           System.out.println("To test if the file, FastaDNA.text located in G drive, has been read:\nSEQUENCE NAME:" + stringFromFile.substring(0, stringFromFile.indexOf(">")));
           System.out.println("SEQUENCE:"+ stringFromFile.substring(stringFromFile.indexOf(">")+1,stringFromFile.length()));
    
    
        }
    
    
    
    
    
        String complement(){
         String com="";
            for(int a=0; a < seq.length(); a++){
                if(seq.charAt(a)=='a'){
                    com= com.concat("t");
                }
                else if(seq.charAt(a)=='t'){
                    com= com.concat("a");
                }
                else if(seq.charAt(a)=='g'){
                    com= com.concat("c");
                }
                else if(seq.charAt(a)=='c'){
                    com= com.concat("g");
            }
          }
            return com;
        }
    
    
         String RNA(String compl){
            String theta= compl.replace("t", "u");
            return theta;
        }
    
    
         String gene(){
            return seq.substring(seq.indexOf("atg"), seq.indexOf("tga")+3);//question
        }
    
    
    
          int length(){
          return seq.length();
          }
    
    
    
          float percentageOfA(){
            float count=0;
            for(int i=0; i<seq.length(); i++){
                if(seq.charAt(i) == 'a'){
                    count++;
                }
            }
            return count/seq.length();
            }
    
    
    
          String[] makeCodons(String gene){
           String make_codons[]= new String[gene.length()/3-gene.length()%3];
        int b=0;
        for( int a=0; a<gene.length()/3-gene.length()%3; a++){
            boolean bool=true;
            while(b<gene.length()-gene.length()%3 && bool==true){
             make_codons[a]= gene.substring(b, b+3);
             b+=3;
             bool=!bool;
             //System.out.println(make_codons[a]);
            }
        }
        System.out.println("\n");
        return make_codons;
        }
    
       void toProtein(String gene){
           String make_codons[]= new String[gene.length()/3-gene.length()%3];
            make_codons= makeCodons(gene);
    
            for(int c=0; c<gene.length()/3-gene.length()%3; c++){
    
                if(make_codons[c].equals("atg"))
                 System.out.println("Methionine");
                else if(make_codons[c].equals("gtt")|| make_codons[c].equals("gtc")|| make_codons[c].equals("gta")|| make_codons[c].equals("gtg"))
                 System.out.println("Valine");
                else if(make_codons[c].equals("att")|| make_codons[c].equals("atc")|| make_codons[c].equals("ata"))
                 System.out.println("Isoleucine");
                else if(make_codons[c].equals("ttg")|| make_codons[c].equals("tta")|| make_codons[c].equals("ctg")|| make_codons[c].equals("cta")|| make_codons[c].equals("ctc")|| make_codons[c].equals("ctt"))
                 System.out.println("Leucine");
                else if(make_codons[c].equals("tct")|| make_codons[c].equals("tcc")|| make_codons[c].equals("tca")|| make_codons[c].equals("tcg")||make_codons[c].equals("agt")|| make_codons[c].equals("agc"))
                 System.out.println("Serine");
                else if(make_codons[c].equals("cca")|| make_codons[c].equals("ccc")|| make_codons[c].equals("cct")|| make_codons[c].equals("ccg"))
                 System.out.println("Proline");
                else if(make_codons[c].equals("act")|| make_codons[c].equals("acc")|| make_codons[c].equals("aca")|| make_codons[c].equals("acg"))
                 System.out.println("Threonine");
                else if(make_codons[c].equals("gct")|| make_codons[c].equals("gcc")|| make_codons[c].equals("gca")|| make_codons[c].equals("gcg"))
                 System.out.println("Alanine");
                else if(make_codons[c].equals("tat")|| make_codons[c].equals("tac"))
                 System.out.println("Tyrosine");
                else if(make_codons[c].equals("cat")|| make_codons[c].equals("cac"))
                 System.out.println("Histidine");
                 else if(make_codons[c].equals("caa")|| make_codons[c].equals("cag"))
                 System.out.println("Glutamine");
                 else if(make_codons[c].equals("aat")|| make_codons[c].equals("aac"))
                 System.out.println("Asparagine");
                 else if(make_codons[c].equals("aag")|| make_codons[c].equals("aaa"))
                 System.out.println("Lysine");
                 else if(make_codons[c].equals("gac")|| make_codons[c].equals("gat"))
                 System.out.println("Aspartic acid");
                 else if(make_codons[c].equals("gaa")|| make_codons[c].equals("gag"))
                 System.out.println("Glutamic acid");
                 else if(make_codons[c].equals("tgt")|| make_codons[c].equals("tgc"))
                 System.out.println("Cystine");
                 else if(make_codons[c].equals("tgg"))
                 System.out.println("Tryptophan");
                 else if(make_codons[c].equals("agt")|| make_codons[c].equals("cga")|| make_codons[c].equals("ctg")|| make_codons[c].equals("cta")|| make_codons[c].equals("cgt")|| make_codons[c].equals("cgg"))
                 System.out.println("Arginine");
                 else if(make_codons[c].equals("gga")|| make_codons[c].equals("ggc")|| make_codons[c].equals("ggt"))
                 System.out.println("Glycine");
    
                 else if(make_codons[c].equals("tga"))
                     System.out.println("STOP CODON");
    
            }
    
       }
        void printing(){
            System.out.println("Name of the sequence:" + name);
            System.out.println("Sequence:" + seq);
            System.out.println("FASTA FORMAT:" + displayInFasta());
            System.out.println("Length of the sequence: " + length());
            System.out.println("Percentage of A: " + percentageOfA());
            System.out.println("Complement:" + complement());
            System.out.println("RNA:" + RNA(complement()) );
            System.out.println("Gene:" + seq.substring(seq.indexOf("atg"), seq.indexOf("tga")+3));
    
            // toProtein k andar hi print karwa dia tha ab kia usy change larun lamda chakar... baaad mein kabhi
        }
    
    
        public static void main(String[] args) {
            DNA c= new DNA();
            DNA dna= new DNA(c.getName(), c.getSeq());
            dna.printing();
    
            //dna.displayInFasta();//"dna." isliye lagaya hai k is k baghair name and seq nonstatic nahn hoty kuz nonstatic variables object ko belong karty hein.is k baghair clear nahn hai k kis object k hien n means k har kliye same hein ie static
            dna.writeToFile(dna.displayInFasta());
            dna.readFromFile();
            //dna.complement();//EK OBJECT SIRF "APNI" FIELDS AS ARGUMENT PAS KARNY KI ZARURAT NAHN RAKHTA, BAKI SAB KUCH PASS KARNY KI ZARURAT HOTI HAI
            //dna.RNA(dna.complement());
            //dna.gene();
    
            //dna.length();
            //dna.percentageOfA();
    
            //dna.printing();
    
            dna.toProtein(dna.gene());
        }
    
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    This is the class which contains code for file read and write.
    The code you have post has too much code besides the file I/O code. If you want to learn how the file classes work it would be better to work with a program that only does file I/O. The other code in your posted code just gets in the way and makes it complicated and difficult to concentrate on the file I/O part.

    Write a small simple program to test the classes and methods you want to use. When you figure out how to use those classes and methods, THEN merge the logic and techniques into the larger program.
    Nazneen Ali likes this.

  7. #7
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    I completely agree with "Norm", the script complicated for a beginner to Java - Basic I/0 .
    Nazneen Ali likes this.

  8. #8
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

Similar Threads

  1. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 02:57 PM
  2. Reading part of a line in a text file
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-04-2009, 07:57 AM
  3. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 01:01 PM
  4. Writing To A Specific Text File Line
    By mokonji in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 08:13 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 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
  •