Results 1 to 12 of 12
- 11-08-2009, 08:27 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
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.
- 11-09-2009, 09:27 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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:
You will have to convert the value you read into an int or Integer because you need to perform an arithmetic operation on it.Java Code:BufferedReader inputFile = new BufferedReader(new FileReader(myFilename)); String value = inputFile.readLine();
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.
- 11-09-2009, 05:10 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
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.
- 11-09-2009, 05:44 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You want to convert a text file to an int? Using what rules?
- 11-09-2009, 05:51 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
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.
- 11-09-2009, 07:42 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Then you need to read the file not convert it. A FileReader could do.
- 11-09-2009, 09:20 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
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?
- 11-09-2009, 11:14 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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:
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.Java Code:BufferedReader inputFile = new BufferedReader(new FileReader("/tmp/myFile.txt")); String value = inputFile.readLine();Last edited by iceagecoming; 11-09-2009 at 11:16 PM.
- 11-10-2009, 03:53 PM #9
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
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?
- 11-10-2009, 04:32 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
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-10-2009, 05:09 PM #11
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
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.
- 11-11-2009, 04:03 PM #12
Member
- Join Date
- Oct 2009
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
FileReader Vs FileInputStream and same goes to output
By unhurt in forum New To JavaReplies: 5Last Post: 02-02-2010, 09:06 AM -
Empty FileInputStream..
By dudejonne in forum New To JavaReplies: 5Last Post: 11-08-2009, 08:21 PM -
FileOutputStream, I dont know what Im doing wrong...
By Addez in forum New To JavaReplies: 6Last Post: 09-14-2009, 10:46 PM -
FileOutputStream gets NotSerializableException
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 09:38 PM -
FileOutputStream question...
By SCS17 in forum New To JavaReplies: 2Last Post: 07-07-2008, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks