Results 1 to 7 of 7
  1. #1
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default how to form a text file

    Hi everybody,

    I try to write a program with java. I figured out something.Now what I want to know is how can I form a file. I mean there is a file.Program reads file at once.Then takes a part of text file,creates a new file,writes the part into that file.But I want to change form of that part.
    For example:

    İnput(in a string):Yesterday.
    Today.
    Tommorrow.
    I wish such an output:Yesterday.Today.Tommorrow

    tOpach,

    Kind regards.
    A stitch in time saves nine:D

  2. #2
    nishant is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Hi pal.......It's Good to see u again. what i understood this might help u.....

    Here is the code......

    public class Name{

    private ArrayList<String> data;
    private Scanner dataScan;

    public Name() {
    data = new ArrayList<String>();
    }
    public static void main(String[] args) {
    new Name().readFile();
    }
    private void readFile() {
    FileInputStream fis =null;
    try
    {
    fis = new FileInputStream("file.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    String Line = null;
    while((Line = br.readLine())!=null){
    System.out.println(Line);
    data.add(Line);
    }
    }

    catch(IOException e){
    System.out.println(e.getMessage());
    }


    writeDataBack();
    }

    public void writeDataBack(){
    FileWriter fw = null;
    int x = 0;
    try
    {
    fw = new FileWriter("file2.txt");
    BufferedWriter out = new BufferedWriter(fw);

    while(x<data.size()){
    out.write(data.get(x));
    out.newLine();
    x++;
    }
    out.close();
    }
    catch(IOException ex){
    System.out.println(ex.getLocalizedMessage());
    }
    }
    }

  3. #3
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    Brother,
    Thanks for simultaneous answer.Now let me add & run it:)

    tOpach,

    Kind regards
    A stitch in time saves nine:D

  4. #4
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    The codes above-mentioned, it not easy to understand:(. I just want to change the form of a given text to a straight line.Thats all:D

    tOpach,

    Kind regards.
    A stitch in time saves nine:D

  5. #5
    nishant is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    hi...
    the last example that i have given to u, it will work like this.

    1).. whatever file u want to read it would read the file using bufferedReader.

    2)..then it write back the content of the file to another file by using BufferedWriter thats it. And make sure that u dont need to create new text file in which u want to write it. it will automatically create the file and write the content.

    its looking difficult but it easy to understand.Anyway i know u r new to java right?......u no need to worry about it, we all are ready to help u

    if this is not that what u want.....then skip the above example.

  6. #6
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    Dear nishant,

    I can see that you really try to help me,so thank you very much:o.In fact I dont want to copy a file to another file(I already do it) .I would like to change the form of it(to a straight line) than write to another file.Thats the part I can not write.


    tOpach,

    Kind regards.
    A stitch in time saves nine:D

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

    Default

    By a "straight line" I assume you mean that you want to remove the newline characters that are in the original file and write the new file without them.
    Look at your program and see if there are any method calls that will write out a newline character. If so, remove it.

Similar Threads

  1. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  2. Struts2 :File upload and div on same form
    By saqib in forum Web Frameworks
    Replies: 2
    Last Post: 08-19-2008, 11:41 AM
  3. Replies: 1
    Last Post: 01-11-2008, 10:06 PM
  4. Displaying text box on MIDlet form
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 10:06 AM
  5. how to upload a file along with html form data
    By pranith in forum Java Servlet
    Replies: 3
    Last Post: 07-30-2007, 02:33 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
  •