Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: IO Questiong

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post IO Questiong

    Hi All,

    I am getting a text file say "a.txt" generated from a tool which gives me some kind of logs. I want to read the data from "a.txt" line by line and write it into "b.txt", deleting the line which I have read from "a.txt" and about to write in "b.txt".

    Also there should be no speed mismatch among read and write operation because it might cause to lost of data.


    Can any one please help me on that technically with code.



    package remotefileaccess;


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.Scanner;

    public class deleteAndReadFile
    {
    public static void main(String[] args)
    {
    //Enter name of the file here
    String filename="C:\\D\\myfiles\\ab.txt";
    String filename1="C:\\D\\myfiles\\pq.txt";
    //Enter starting line here
    int startline=1;
    //Enter number of lines here.
    int numlines=1;

    deleteAndReadFile now=new deleteAndReadFile();
    now.delete(filename,startline,numlines,filename1);
    }
    void delete(String filename, int startline, int numlines,String filename1)
    {
    try
    {
    BufferedReader br=new BufferedReader(new FileReader(filename));
    Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(filename1, true), "UTF-8"));
    //String buffer to store contents of the file
    StringBuffer sb=new StringBuffer("");
    Scanner input = new Scanner(new File(filename));
    //Keep track of the line number
    int linenumber=1;
    String line;
    while(input.hasNextLine()){
    while((line=br.readLine())!=null)
    {
    //Store each valid line in the string buffer
    if(linenumber<startline||linenumber>=startline+num lines)
    { sb.append(line);
    sb.append(System.getProperty("line.separator"));

    }
    else
    {
    writer.append("**"+line);
    writer.append(System.getProperty("line.separator") );
    }
    startline++;
    linenumber++;
    }

    if(startline+numlines>linenumber)
    System.out.println("End of file reached.");

    br.close();
    writer.close();

    FileWriter fw=new FileWriter(new File(filename));
    //Write entire string buffer into the file
    fw.write(sb.toString());
    fw.close();
    }

    }
    catch (Exception e)
    {
    System.out.println("Something went horribly wrong: "+e.getMessage());
    }
    }
    }
    Thank you
    Rohant
    Last edited by rohantjoshi101@gmail.com; November 15th, 2013 at 01:07 AM. Reason: code

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: IO Questiong

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: IO Questiong

    Hello.
    If we want to modify an existing file by deleting its content this is the only basic solution.
    (a) copy the data from the source file to a target file discarding the unrequired data.
    (b) delete the source file.
    (c) rename the target file back to that of source file.

    Does this solve your problem?

    syed.