jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes String, read and write file problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "String, read and write file problem..." Watch "String, read and write file problem..." New topic
Author

String, read and write file problem...

Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Dear all.
I have the following file

Animal; Light; Width; Height; Size // line 1
dog; lamp; 10; 20; 15 // line 2

Acc; Name; Phone; Address; ID; // line 3
Test; Peter; 900234; 10 Le Duan; 5 // line 4



So i would like to store the line 1 and line 3 with variables:
animal; light; width; height; size....
I would like to retrieve the data: animal = dog; light = lamp; width = 10; height = 20; size =15..... id = 5;
So please help me to slove my problem.
Thanks and best regards,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
How far have you gotten? It's hard to tell if you're stuck at reading files, parsing text or maybe digging into reflection to set variables. Show us what you have and we can see right where you're stuck.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you're not sure where to begin, and you're using JDK 5 or later, I recommend you read Sun's tutorial on java.util.Scanner.


"I'm not back." - Bill Harding, Twister
Omar Omran
Greenhorn

Joined: Aug 24, 2007
Posts: 5
you can paste the code here and point to the problem


<a href="http://www.titanic-hosting.com" target="_blank" rel="nofollow">News In Java(1)</a><br /><a href="http://www.titanic-hosting.net" target="_blank" rel="nofollow">News In Java(2)</a><br /><a href="http://www.titanic-hosting.org" target="_blank" rel="nofollow">News In Java(3)</a><br /> <br /><a href="http://myliferay.net" target="_blank" rel="nofollow">Build Your Personal Site</a>
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Ok here is my text file.
Animal; Light; Width; Height; Size // line 1
dog; lamp; 10; 20; 15 // line 2

here is my code:



and here is main class:



1- It doesn't report the error but still not show on the console.
2- For now the file have form following:
12:14:16; acc ; name; id; size

The first field is time format (hh:mm:ss)so i cannot use startWith() method , because it is change follow the time. Please suggest me the solution
Sorry for my bad english.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Tran]: 1- It doesn't report the error but still not show on the console.

This is very important - your catch block doesn't do anything. It could be catching an error here, but since there's no code inside the catch block, it does nothing with the error, and then the program continues. This is very bad - it hides the error from the programmer (you).

To fix this you can either (a) put e.printStackTrace() inside the catch block (or some other code which reports the error somehow), or (b) remove the try/catch and just let the exception be thrown. You may need to add some more exceptions to the "throws" clause if you do this.

[Tran]: 2- For now the file have form following:
quote:
12:14:16; acc ; name; id; size

The first field is time format (hh:mm:ss)so i cannot use startWith() method , because it is change follow the time. Please suggest me the solution


You could try something like

That assumes that the only lines that start with digits (numbers) are the lines that start with times. If there are also other types of lines with numbers, then you will need something more complex. A regular expression would be a good general solution. That may take some time to understand if you're not familiar with regular expressions, but they're very useful to know about. I don't know if you need to know about them for this problem, but you should learn about them eventually.

Some other notes:

If this is intended to be a constant, you probably want to add "final" to the modifiers here.

Is this on Windows, or some sort of Unix? On Windows you probably want to use \\ rather than /. Java may "correct" this for you in some cases, but in general it's easier to be consistent in the first place - use the separator that's appropriate for your system.

There's no reason for "line" to be declared as part of the class. It's only used within one method, and you don't need to remember any previous value from the last time the method was called. So it's better to just declare the variable within that method, as close as possible to where it's used.

While this works, it's generally a very inefficient way to manipulate Strings. What happen is, it gets converted into something like this:

Every time you use + in a new expression, it creates a new StringBuilder to get the result. It's better (and simpler) to just put these all in one line:

This is equivalent to

which is much less work for the JVM.
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
you are the man Jim Yingst , thanks thanks you very much .
if for now my file have format following:
9:12:05;MS Healthy Message

030000002f130200000000000800450000800000000001119f7a0a000000e0e02f13c5dac5da006c00000000000003030000000000

04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

00000000000000000000000000000000000000000000000000000000000000000000000000
9:13:58;MS Healthy Message
0300000032330200000000000800450000800000000001119c5a0a000000e0e03233c5dac5da006c0000000000000303000000000

00400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

000000000000000000000000000000000000000000000000000000000000000000000000000


For now, i have 3 fields:
1- formatTime variable (hh:mm:ss);
2- a typeMsg variable is type Message.
3- a byte array with byteArr: (private byte byteArr
"030000002f130200000000000800450000800000000001119f7a0a000000e0e02f13c5dac5da006c000000000000030300

00000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000000000000000000000000"
I dont know how to show the byteArr.
thanks and regards,
great forum
 
GeeCON Prague 2014
 
subject: String, read and write file problem...