Results 1 to 12 of 12
  1. #1
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Cool FileOutputStream > int > FileInputStream

    Hi people, I have a server with FTP. Now there is a textfile on my server. In the textfile there is a number, just a number. I retrieve the file, and put it in a FileOutputStream, now I want to convert it to an int and higher the number with one. So for example from 445 to 446. Then I need to convert it to a FileInputStream, so I can send it back.

    I've been looking on Google and stuff, but I cannot find this. Could someone help me?

    Also, it doesn't needs to be converted to an int, just as long as the number inside the textfile gets changed one higher.

    Thanks.

  2. #2
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    With respect to your program, you will need to read in the file using a FileInputStream, or you can use a BufferedReader, and then read in the value. For example:
    Java Code:
    BufferedReader inputFile = new BufferedReader(new FileReader(myFilename));
    String value = inputFile.readLine();
    You will have to convert the value you read into an int or Integer because you need to perform an arithmetic operation on it.

    Once you have the new value, you will need to write out the value using FileWriter, and overwrite the previous value in the file.
    Java Code:
    FileWriter fw = new FileWriter(myFilename);
    fw.write(newValue);
    fw.flush();
    Last edited by iceagecoming; 11-09-2009 at 09:31 AM.

  3. #3
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    I'm sorry but I use this code, from the apache FTPClient:
    Download file from FTP server : Ftp**Network Protocol**Java
    It writes the file into a FileOutputStream. And now I want to convert a index.txt from my FTP server to an int.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You want to convert a text file to an int? Using what rules?

  5. #5
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    I am not sure what you mean with what rules. The only thing inside the index.txt is a number. Like for example 45643. Now I need to store that number in an int. Thats it.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Then you need to read the file not convert it. A FileReader could do.

  7. #7
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    Okay, I've made a little test, but I get an error at this part:
    FileReader fileReader = new FileReader(fos); //fos is a FileOutputStream.

    And it is because instead of fos I should input a File, but I don't know how?

  8. #8
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    You should look at the java doc for FileReader (sorry, I don't have enough points to post the link). You need to give it a File or just a String which contains the filename. Like in the example I gave you, you could just have a string:
    Java Code:
    BufferedReader inputFile = new BufferedReader(new FileReader("/tmp/myFile.txt"));
    String value = inputFile.readLine();
    Like I said before, you need to read the contents of the file, store the value as an integer, perform the arithmetic on the value and write that new value back out to the file.
    Last edited by iceagecoming; 11-09-2009 at 11:16 PM.

  9. #9
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    Yes but like I already told, I don't have a file!

    Look at this code:

    String filename = "index.txt";
    fos = new FileOutputStream(filename);

    client.retrieveFile("/" + filename, fos);
    fos.close();

    The file is inside the fos right? How do I get the FileReader to read that?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    The moment you create your FileOutputStream any previous content (if any) of the file is lost for the posterity. Is this what you want?

    kind regards,

    Jos

  11. #11
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    I don't know what postery means, but all I want to do is read that textfile from my server, change it, store it back. I use the Apache FTPClient and the only way to retrieve a file is storing it in a FileOutputStream, there is no previous content it the FileOutputStream so that wouldn't matter.

  12. #12
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    don't know any way to convert a FileOutputStream to something else. And .retrieveFile can only store it in a fos.

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2010, 09:06 AM
  2. Empty FileInputStream..
    By dudejonne in forum New To Java
    Replies: 5
    Last Post: 11-08-2009, 08:21 PM
  3. Replies: 6
    Last Post: 09-14-2009, 10:46 PM
  4. FileOutputStream gets NotSerializableException
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 09:38 PM
  5. FileOutputStream question...
    By SCS17 in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 05:30 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
  •