Welcome to Dream.In.Code
Become a Java Expert!

Join 149,597 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,897 people online right now. Registration is fast and FREE... Join Now!




Reading and Writing between two files

 
Reply to this topicStart new topic

Reading and Writing between two files

debdeep123
8 Oct, 2007 - 02:15 AM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 12


My Contributions
hi , i want to read a text file and write it into another text file. condition is that i read the file upto 10 words and write into a file then read next 10 words and write into the next line of the same file where i wrote before. eg:-
read file A contains:
ram is a good boy he goes to school daily and his father is an engineer

Write into file B:

ram is a good boy he goes to school daily--1st line as i mentioned contains 10 words

and his father is an engineer---2nd line of the file.

Kindly help me out.
User is offlineProfile CardPM
+Quote Post

chuck87
RE: Reading And Writing Between Two Files
8 Oct, 2007 - 08:24 AM
Post #2

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
You 've got to post some code if you want to get help.I can help you with this assignment but please show some effort
User is offlineProfile CardPM
+Quote Post

debdeep123
RE: Reading And Writing Between Two Files
8 Oct, 2007 - 10:16 PM
Post #3

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 12


My Contributions
QUOTE(chuck87 @ 8 Oct, 2007 - 09:24 AM) *

You 've got to post some code if you want to get help.I can help you with this assignment but please show some effort


ok..here is my code..
CODE

import java.io.*;
class Filereading
{
    public static void main(String args[])
    {
    String Inp = new String();
    String Temp = new String();
    String FinalInp = new String();
     int lines = 0;
     int words = 0;
    int ch;
    try {
            File file = new File("D:/javaexercise/query.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader buff = new BufferedReader (fileReader);
            boolean eof = false;
            while (!eof)
                {
                  Inp = buff.readLine();
                  if (Inp == null)
                  eof = true;
                  else
                  {
                    Temp = Inp;
                    FinalInp = FinalInp + " " + Temp;
                    Temp = "";

                  
                   }
                }

                      try
                         {
                          boolean appendData=true;
                          FileWriter fot = new FileWriter("D:/javaexercise/final.txt",appendData);
                          System.out.println("Write Query 1 ");
                           fot.write(FinalInp,0,FinalInp.length());
                            System.out.println("Write Query 2 ");
                             fot.close();
                         }
                        
                        catch(Exception e)
                          {
                           System.out.println("Exception in side Else part"+e);
                          }

            buff.close();
        }
            catch (Exception e)
                {
                  System.out.println ("Error ...." + e.toString());
                }
            
                try{
          
            File file2 = new File("D:/javaexercise/final.txt");
            FileReader fileReader2 = new FileReader(file2);
             StreamTokenizer st1 = new StreamTokenizer(fileReader2);
             st1.resetSyntax();
             st1.wordChars(33,255);
             st1.eolIsSignificant(true);
             while(st1.nextToken() != st1.TT_EOF)
        {
                 switch(st1.ttype)
            {
                     case StreamTokenizer.TT_EOL:
                         lines++;
                         break;
                     case StreamTokenizer.TT_WORD:
                         words++;
                         break;
            }
        }

        System.out.println("No of lines read is"+lines);
        System.out.println("No of Words read is "+words);
                }
                catch(Exception e){}
      
           try{

               File file1 = new File("D:/javaexercise/final.txt");
            FileReader fileReader1 = new FileReader(file1);
            BufferedReader buff = new BufferedReader (fileReader1);
            boolean eof = false;
            String str="Insert into fgt_mesg_table values(555555,1,\'";
            String st="\',400)";
            String query="";
            while (!eof)
                {
                  Inp = buff.readLine();
                  if (Inp == null)
                  eof = true;
                  else
                  {
                      query=str+Inp+st;
                      System.out.println("Write Query from file is-->"+query);
                   }
                }
         if(words >8)
           {
             System.out.println("No of words are more then 8");
                try
                {
                  boolean appendData=true;
                  FileWriter fot = new FileWriter("D:/javaexercise/final2.txt",appendData);
                  System.out.println("Write Query 1 ");
                   //fot.write(query,0,15);
                   fot.write(query,0,query.length());
                  
                    System.out.println("Write Query 2 ");
                     fot.close();
                 }
                        
                catch(Exception e)
                {
                 System.out.println("Exception in side Else part"+e);
                }
            }
           else
               {
                  System.out.println("No of words are NOT more then 8");

                    try
                     {
                      boolean appendData=true;
                      FileWriter fot = new FileWriter("D:/javaexercise/final2.txt",appendData);
                      System.out.println("Write Query 1 ");
                       fot.write(query,0,query.length());
                       //fot.write(query,0,query.length());
                        System.out.println("Write Query 2 ");
                         fot.close();
                    }
                        
                   catch(Exception e)
                     {
                       System.out.println("Exception in side Else part"+e);
                     }

              }
            buff.close();
        }
            catch (Exception e)
                {
                  System.out.println ("Error ...." + e.toString());
                }

          }
}


User is offlineProfile CardPM
+Quote Post

chuck87
RE: Reading And Writing Between Two Files
9 Oct, 2007 - 07:01 AM
Post #4

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
Because your code is too big I will post the code I created.You did a really good job though.

CODE

import java.io.*;

class Prog
{
    public static void main(){
        try{
            File source =new File("src.txt");
            File destination = new File("dest.txt");
            FileReader inp = new FileReader(source);
            BufferedReader inbuff = new BufferedReader(inp);
            FileWriter outp = new FileWriter(destination);
            BufferedWriter outbuff = new BufferedWriter(outp);
            
            int inChar;
            int counter=0;
            while((inChar=inbuff.read())!=-1)
            {
                if(inChar==' ')
                    counter++;
                
                char outChar = ((char)inChar);    
                outbuff.write(outChar);
                if(counter==10)
                {
                    counter=0;
                    outbuff.newLine();
                }
            }
            inbuff.close();
            outbuff.close();
        }catch(FileNotFoundException fnf){
            System.out.println("File not found in current working directory");
        }catch(IOException ioe){
            System.out.println("IO exceptioin");
        }
    }
}


This post has been edited by chuck87: 9 Oct, 2007 - 07:03 AM
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Reading And Writing Between Two Files
9 Oct, 2007 - 07:05 AM
Post #5

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Chuck, can you explain what you did so the OP understands... Thanks for helping!
User is offlineProfile CardPM
+Quote Post

chuck87
RE: Reading And Writing Between Two Files
9 Oct, 2007 - 10:50 AM
Post #6

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
QUOTE(skyhawk133 @ 9 Oct, 2007 - 08:05 AM) *

Chuck, can you explain what you did so the OP understands... Thanks for helping!


Sorry for not explaining the code before.I assumed that if debdeep didn't understand something he would make a post with he questions.I will do my best in explaining I am not really fluent in english though

The method read() takes every character in the file and returns it as an integer therefore I use the variable inChar which is an integer to store every character that is read from the file.
Inside while the integer value of inChar is compared with -1 which stands for eof.
Concerning the if below the value of inChar is compared with the integer value of a blank space.When the variable counter reaches 10 I use the command outbuff.newLine() so that the rest words are written in the next line of the destination file
Concerning this part of the code
CODE

        char outChar = ((char)inChar);    
        outbuff.write(outChar);
        

in the outChar variable is stored the value of inChar as a character and not as an integer and then written in the destination file with the next command

I hope I helped with this explanation

This post has been edited by chuck87: 9 Oct, 2007 - 10:52 AM
User is offlineProfile CardPM
+Quote Post

debdeep123
RE: Reading And Writing Between Two Files
10 Oct, 2007 - 02:06 AM
Post #7

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 12


My Contributions
QUOTE(chuck87 @ 9 Oct, 2007 - 08:01 AM) *

Because your code is too big I will post the code I created.You did a really good job though.

CODE

import java.io.*;

class Prog
{
    public static void main(){
        try{
            File source =new File("src.txt");
            File destination = new File("dest.txt");
            FileReader inp = new FileReader(source);
            BufferedReader inbuff = new BufferedReader(inp);
            FileWriter outp = new FileWriter(destination);
            BufferedWriter outbuff = new BufferedWriter(outp);
            
            int inChar;
            int counter=0;
            while((inChar=inbuff.read())!=-1)
            {
                if(inChar==' ')
                    counter++;
                
                char outChar = ((char)inChar);    
                outbuff.write(outChar);
                if(counter==10)
                {
                    counter=0;
                    outbuff.newLine();
                }
            }
            inbuff.close();
            outbuff.close();
        }catch(FileNotFoundException fnf){
            System.out.println("File not found in current working directory");
        }catch(IOException ioe){
            System.out.println("IO exceptioin");
        }
    }
}



hey..thanks a lot..

User is offlineProfile CardPM
+Quote Post

debdeep123
RE: Reading And Writing Between Two Files
10 Oct, 2007 - 03:07 AM
Post #8

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 12


My Contributions
QUOTE(debdeep123 @ 10 Oct, 2007 - 03:06 AM) *

QUOTE(chuck87 @ 9 Oct, 2007 - 08:01 AM) *

Because your code is too big I will post the code I created.You did a really good job though.

CODE

import java.io.*;

class Prog
{
    public static void main(){
        try{
            File source =new File("src.txt");
            File destination = new File("dest.txt");
            FileReader inp = new FileReader(source);
            BufferedReader inbuff = new BufferedReader(inp);
            FileWriter outp = new FileWriter(destination);
            BufferedWriter outbuff = new BufferedWriter(outp);
            
            int inChar;
            int counter=0;
            while((inChar=inbuff.read())!=-1)
            {
                if(inChar==' ')
                    counter++;
                
                char outChar = ((char)inChar);    
                outbuff.write(outChar);
                if(counter==10)
                {
                    counter=0;
                    outbuff.newLine();
                }
            }
            inbuff.close();
            outbuff.close();
        }catch(FileNotFoundException fnf){
            System.out.println("File not found in current working directory");
        }catch(IOException ioe){
            System.out.println("IO exceptioin");
        }
    }
}



hey..thanks a lot..

i have understood..no problem!! Thanks. Now the problem is what ever i will write to dest.txt from src.txt by default "insert into table a values(1994512,1,' " this to be added at the begining and "',400)" to be added at the end of each line. Remember the word count of 10 is correct only no need to disturb that code. So, all the lines of dest.txt will be like:-
insert into table a values(1994512,1,' 10 words of src.txt(what ever it is)' ,400)
the above mentioned format. Kindly help me out..hope this time i do not need to show my effort,
as in my big code i have already tried for that. But could not make it. So please please help me out.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month