Re: Problem with file I/O
If you are simply moving the data and not doing anything with it in between reading and writing then just use the Input and Output streams and a byte[] buffer.
No need for all those other in between streams, which may or may not be the cause of your problem.
Code:
.. open the streams
byte[] buffer = new byte[8192]; // 8k buffer
int byteCount; // so we know how much was read in each loop
while((byteCount = fstream.read(buffer)) > -1) {
outstream.write(buffer, 0, byteCount);
}
.. close it all off (in a finally block).
That will read anything from a to b, and take only a handful of k to do it (assuming b is not in memory).
You seemed to be overcomplicating things.
Re: Problem with file I/O
I have to read a large file, line by line, then divide by separator(convert to longint) and put it in a data structure, print out any line the user wishes. The data structure is clear, im just curios about how to make the reading part. Thanks.
Re: Problem with file I/O
OK.
Take out all those streams and do:
Code:
BufferedReader br = new BufferedReader(new FileReader("your file name"));
similarly for the writer (note this really ought to be done such that you can close the readers in a finally block).
I say this because I have just noticed you are closing the (utterly unecessary) DataOutputStream.
You are not closing the BufferedWriter.
The BufferedWriter which (as its name suggests) buffers the output.
So you close the underlying stream, which has no effect on anything which may still be held in that buffer.
Doing the above (just declaring the BufferedReader/Writer) will prevent this from happening.
Re: Problem with file I/O
Also, at the end close br and br2 instead of in and out; there may still be data in those bufferers.
kind regards,
Jos
Re: Problem with file I/O
Thanks guys, did all the above and everything is how it should be. Got to do some reading on file handling or wait till the lectures touch the subject.
One last question regarding dividing the string up. What should i use? As far as googling told me, that the best option should be using split(), is that right? Or maybe use chartoarray to make things complicated?
Re: Problem with file I/O
Quote:
Originally Posted by
MustSeeMelons
Thanks guys, did all the above and everything is how it should be. Got to do some reading on file handling or wait till the lectures touch the subject.
One last question regarding dividing the string up. What should i use? As far as googling told me, that the best option should be using split(), is that right? Or maybe use chartoarray to make things complicated?
split is the best, for example you have in your text "lol=5" you can create an array (We'll call it varHold) and you can do "varHold = string.split("=")" and that way
varHold will have two strings varHold[0] would me "lol" and varHold[1] would me "5"
Re: Problem with file I/O
Quote:
Originally Posted by
JosAH
Also, at the end close br and br2 instead of in and out; there may still be data in those bufferers.
kind regards,
Jos
Oi...that's what I said!
:p
Re: Problem with file I/O
Thanks, split was really simple, program is running almost perfectly, but i have a question regarding Exceptions.
How do i create custom mesages? I need 3 in total, file not found, delimiter not found and the the user asks to print a line that does not exist.
File not found works by default, but the other two are messing eash other around. Should i create my own exceptions? Cant even asks the question properlly..
Re: Problem with file I/O
Try and catch
Code:
try{
Scanner lol = new Scanner(new File("file.ini"));
catch (Exception e){
System.out.println("File not found");
}
Easy enough :D
Re: Problem with file I/O
Lol indeed, i know that much :D Here is the problem. If no file is found, there is an exception, if the delim is wrong, there has to be an exception, and if out of bounds occurs, there has to be an exeption. If the delim is cought, it outputs it all the time and just stops.
Code:
public void magic()
{
ListOfRows RowList=new ListOfRows();
try
{
BufferedReader buffer=new BufferedReader(new FileReader(location), 2048);
String Line;
while((Line=buffer.readLine())!=null)
{
// if(Line.matches(delim)==false)
// {
// buffer.close();
// throw new Exception("Delim fault");
// }
ListOfNumbers NumberList=new ListOfNumbers();
String[] Temp=Line.split(delim);
for(int i=0;i<Temp.length;i++)
{
Long StringNumber=Long.parseLong(Temp[i]);
Number NewNumber=new Number(StringNumber);
NumberList.add(NewNumber);
}
RowList.add(NumberList);
}
buffer.close();
System.out.println("Row count: "+RowList.size);
System.out.println("Number count in row: "+RowList.getNode(rowPrint).Cur.size);
RowList.getNode(rowPrint).Cur.print(order);
System.out.println();
}
catch(Exception e )
{
System.err.println("Error: "+e.getMessage());
}
Re: Problem with file I/O
Well of course it will close, because if anything inside of the Try fails, it stops that to try. You should do multiple tries
Re: Problem with file I/O
Like:
Try
find file
fileFound = true
catch "could not find file"
if fileFound then
do something with the file
Re: Problem with file I/O
Yeah, understood that, thanks. If there is a possibility to throw somwthing, i just would have to make a seperate try? A try, for finding the dile, a try, to check the delimiter and a try for out of bounds? And another question, if i have two exceptions throwable in one try, then i need only one catch? Because if i had two, both would catch them?
Re: Problem with file I/O
The one that fails will return an exception
But make sure you add the Try only where it really has to be