Hello Guys, Hope youre all well :) Please can someone kindly tell me how I can write to a specific line. The value to be written needs to be in long and not as a string. The File is as follows:

1324214082786
1313131
Paid
0.0

the seconds line is time in miliseconds, i need to replace that to current miliseconds using filewriter. please tell me how and ive included the listener method to which this needs to be done.i m not experiencing error but it replaces whole file with a string.

/*********************Pay Button Listener***************************************/
	public class payListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			
			
			String fileName2 = "Ticket/"+ticketIDNumber+".dat";
			File ticket2 = new File(fileName2);
			if(ticket2.exists()){
				try{
					FileReader fileReader = new FileReader(fileName2);
					BufferedReader bufferedReader = new BufferedReader(fileReader);
					bufferedReader.readLine(); //OMIT Entrance Time 
					
					FileWriter fw = new FileWriter(fileName2);
					BufferedWriter bufferedWriter = new BufferedWriter(fw);
					//fw.write("Hello");
				
					bufferedReader.close();
					fileReader.close();
					fw.close();
				}
				catch(IOException f){
					System.out.println("Error in Setting Payment Time for ticket");
				}		
			}
			
			}
		}
	
	
	/*********************Pay Button Listener***************************************/

Recommended Answers

All 21 Replies

no can do as far as I know ..
just cast your long to a String, and write that.
when you read your file, cast the String you write back to a long.

Is "cast" the right word to use here?
Would the more generic term: convert be better?
I think of cast as a coding technique: a datatype in parenthesis () before an expression.

nyes, propably convert is better suited for the situation :)

ok so how about writing to 2nd line only?

what do you mean, write to second line only?

how to replace the existing data of the 2nd line of the file, how do perform the Fwrite function to write on the second line only keeping others same as they are.

Member Avatar for hfx642

You can think of it THIS way...
BufferedReader and BufferedWriter are to read and write text files (ie strings).
Sounds like you want to do ObjectInputStream and ObjectOutputStream to read and write binary files.

could you look at my code and just show me how to write it to 2nd line of file?

You can not write just the second line easily.
You need to read in the whole file, change the second line and write it all out.
The bytes in the file are contiguous (right next to each other). You would need the new second line to be exactly the same size as the current line. For example if the file contained:
111111222233334444
If the above is your file and you want to replace the 2222, with AA and you wrote over the current file what would you do with the extra 22?
You have to shift all the remaining bytes in the file left 2 bytes.
What if you want to replace 2222 with BBBBBBBB
then you have to shift right the whole file by the extra 4 bytes.

Sounds like you want to do ObjectInputStream and ObjectOutputStream to read and write binary files.

I think this is a slip of the keyboard - should be DataInputStream and DataOutputStream ;)

Member Avatar for hfx642

No... It's NOT a slip of the keyboard.
I have been using ObjectInputStream and ObjectOutputStream for years... and it works.
But, gee... Now you got me thinking, James.
I'm not a Java expert. (Actually... I'm an Oracle expert!)
Should I be using DataInputStream and DataOutputStream?
If so... Please tell me why?

OOS writes in a format with info about the Objects you are writing, which is used by OIS when reading them in. DOS and DIS just write the bytes of raw data. Having said that, if you just write primitives and UTF strings then maybe they are the same? I don't know.
I've always used OOS/OIS when sending objects from A to B (both Java), and DOS/DIS when writing/reading files that are shared with non-Java programs.
Anyway, sorry if maligned your keyboard skills unfairly!

ps: I found this on the internal format of an OOS. It describes a lot of metadata and block structure that is not present in a DOS
http://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html

Member Avatar for hfx642

Anyway, sorry if maligned your keyboard skills unfairly!
Ya make me laugh, James... Ya make me laugh!!!
If I could write files without object info, that would be great.
I'll certainly look into that.
It would make it a LOT easier when updating/converting file structures!

I am still experiencing difficulty in replace content of seconds line, would someone be able to give me an example using while loop or something and readline() function?

There's one key point that's unclear from your posts... what is the format of that second line? Is it a string of ordinary text, representing a number in a human-readable form, or is it a Java long written as an 8-byte binary value that people cannot read?

There's one key point that's unclear from your posts... what is the format of that second line? Is it a string of ordinary text, representing a number in a human-readable form, or is it a Java long written as an 8-byte binary value that people cannot read?

sorry for the unclear post, the seconds line is i believe a long value currently in the file, this is my random milisecond time. i want to replace it to current actual milisecond and i want it to be long so i

can the maths. currently a setter method is used as follows:

public void setpaymentTime(long paymentTime){
		this.paymentTime = 1313131;
	}

now i am trying to replace the 1313131 to System.currentimemillis() value. instiantiation of the class which has the setter method is not working. ive tried this:

TicketInfo replace = new TicketInfo();
replace.setpaymentTime (System.currentTimeMillis());

giving the following compiler errors:;

PaymentNewGUI.java:142: cannot find symbol
symbol  : constructor TicketInfo()
location: class TicketInfo
                        TicketInfo replace = new TicketInfo();

hope that clears it up :)

No. I understand that the data starts out as a long, and eventually needs to be used as a long, but how is it stored in the file? When you look in the file do you see 1313131 or do you see some gibberish binary values?

No. I understand that the data starts out as a long, and eventually needs to be used as a long, but how is it stored in the file? When you look in the file do you see 1313131 or do you see some gibberish binary values?

the file content is as follows:

1324254875443
1313131
Paid
0.0


1313131 is random unwanted number, required to be replaced to current time milliseconds. i want to keep rest as same, but replace 2nd line.

OK, so it's in text/string format.

As you have already been told, you can't just overwrite line 2 in the file. You have to read the file in, update it , and write it back out again.

What follows isn't the slickest or most expert way to do that, but it breaks it down into three easy steps for you to code one at a time.

1.1 Create a String array big enough to hold all the lines of the file (4?)
1.2. In a loop read each line of the file into the array, one line per array element.
1.3 Print the array [ System.out.println(Arrays.toString(myArray)); ] to confirm that's working. Debug as necessary.

When that is working:
2.1 Convert your new long value to a String
2.2 Use that to replace the second element in your array
2.3 Print the array [ System.out.println(Arrays.toString(myArray)); ] to confirm that's working. Debug as necessary.

When that is working:
3.1 In a loop write each element of the array to the file, one line per array element.

commented: can't make it much more clear than this +12

OK, so it's in text/string format.

As you have already been told, you can't just overwrite line 2 in the file. You have to read the file in, update it , and write it back out again.

What follows isn't the slickest or most expert way to do that, but it breaks it down into three easy steps for you to code one at a time.

1.1 Create a String array big enough to hold all the lines of the file (4?)
1.2. In a loop read each line of the file into the array, one line per array element.
1.3 Print the array [ System.out.println(Arrays.toString(myArray)); ] to confirm that's working. Debug as necessary.

When that is working:
2.1 Convert your new long value to a String
2.2 Use that to replace the second element in your array
2.3 Print the array [ System.out.println(Arrays.toString(myArray)); ] to confirm that's working. Debug as necessary.

When that is working:
3.1 In a loop write each element of the array to the file, one line per array element.

thanks james, is there any chance of you giving an example using java code, its makes it easier to understand arrays. :(

I broke that down into steps so small and so simple that I can't really give any more detail without coding it for you. If you're not familiar with arrays yet then create four String variables for line1, line2 etc and use those instead of the array. (In which case you won't use any loops either).
In the end you will only learn how to do this by actually doing it yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.