Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default

    Hi Susie,

    Try my code below, save as FileConvertor.java.
    Run with argument c:\fromfile.txt and c:\tofile.txt [change it depend on your input and output file names]

    Answer to your question, bw.newLine(); will provide the new line feature [see my code below]
    My try-catch arrangement is better and you can have a look.
    It is better not to hard-code the file name, so you need to pass in 2 parameters while running the code.
    You can see the method and variable naming is more meaningful.
    The readWriteTxtFile should not return String.

    If you have any issue regarding the code, kindly send a reply.

    Thanks
    Jing-yi



    import java.io.*;
    import java.util.ArrayList;

    public class FileConvertor
    {
    public void readWriteTxtFile(String inputFileName,String outputFileName){

    FileReader fr = null;
    BufferedReader br = null;
    FileWriter fw = null;
    BufferedWriter bw = null;

    try{
    fr = new FileReader(inputFileName);
    br = new BufferedReader(fr);
    fw = new FileWriter(outputFileName,true);
    bw = new BufferedWriter(fw);

    String line="";

    while((line=br.readLine())!=null){

    StringBuilder prova = new StringBuilder(line);
    line=(prova.delete(16,40).toString());

    line=line.replace(',','.');
    line=line.replace(';',',');
    line=line.replace("09:03","0900");

    bw.write(line);
    bw.newLine();
    }
    }
    catch (FileNotFoundException e1)
    {
    System.err.println("Error! File not found "+e1.toString());
    }
    catch (IOException e2)
    {
    System.err.println("Error! Problem while read/write "+e2.toString());
    }
    catch(Exception e3)
    {
    System.err.println(e3.toString());
    }
    finally
    {
    try{
    if(bw!=null)
    {
    bw.close();
    }

    if(fw!=null)
    {
    fw.close();
    }

    if(br!=null)
    {
    br.close();
    }

    if(fr!=null)
    {
    fr.close();
    }
    }
    catch(IOException e4)
    {
    System.err.println("Error! Problem while close "+e4.toString());
    }
    }

    System.out.println("Read Write process done without error.");
    }

    public static void main(String argv[])
    {
    FileConvertor fileConvertor = new FileConvertor();
    fileConvertor.readWriteTxtFile(argv[0], argv[1]);
    }
    }
    Last edited by jing-yi; 06-10-2011 at 07:34 PM.

  2. #22
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    Hi Jing-yi,

    thank you very much for your code; my one is a merge from three different methods, your is more clear and well designed.

    I know I have a lot to learn, and this is a good lesson!

    Thanks to all for any suggestions and help.

    Regards,

    Susie

  3. #23
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please don't spoon feed code, also, if you do post code, please wrap it in code tags. Rather than giving her code, she would get much more help if you pushed her in the right direction.

    I haven't read the spoonfed code, but @Susie: the pseudo code should like like this

    Java Code:
    open streams for reading and writing
    loop
      read a single line and store it
      perform conversions on single line
      write converted line output file
    end loop

  4. #24
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Try a PrintWriter instead of a BufferedWriter.

  5. #25
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    Hi Tolls,

    I've read about PrintWriter compared to BufferedWriter but it seems BufferedWriter better suits my needs.

    The code works fine and I've designed a simple GUI to accomplish my txt conversion with few mouse click
    Passing method as argument-a00266.jpg

    I've set a jComboBox where I would like to list all files in a fixed folder (in example, C:\mydata\dax.txt), is there a method for this? I know jDialog shows all folder and directories, but I would prefer to use jComboBox if possibile.

    I will then use getSelectItem() method to get the txt file to be converted.

    Regards,

    Susie

  6. #26
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I've set a jComboBox where I would like to list all files in a fixed folder (in example, C:\mydata\dax.txt), is there a method for this?
    Check the API for java.io.File. You'll be wanting a method that returns a File[] array. Notice that you can filter the list, if that's appropriate to your need.

    db

  7. #27
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Quote Originally Posted by susieferrari View Post
    Hi Tolls,

    I've read about PrintWriter compared to BufferedWriter but it seems BufferedWriter better suits my needs.
    Sorry, I was thinking in terms of the newline.
    PrintWriter comes with a println() method.
    But if it's all working, then feel free to ignore.
    :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. perfect method parameter/argument match rejected. why?
    By collegestudent115 in forum Advanced Java
    Replies: 5
    Last Post: 10-31-2010, 06:55 PM
  2. Passing an array to a method.
    By twcast in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 09:13 AM
  3. Method as an argument?
    By StokedOnMe in forum New To Java
    Replies: 13
    Last Post: 09-18-2009, 06:29 AM
  4. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 05:08 AM
  5. Replies: 4
    Last Post: 03-03-2009, 09:39 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
  •