Save data to a File, i need help for my work please

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Save data to a File, i need help for my work please

Postby scb » Fri Oct 26, 2007 9:09 pm

Hi!

have you any idea how i can save data to a file? i tryed some solutions

i.e using PrintStream out = new PrintStream( new FileOutputStream("data.txt") );

but i have this error from eclipse

Lego.java:36: cannot find symbol
symbol : class PrintStream
location: class Lego
PrintStream out = new PrintStream( new FileOutputStream("data.txt") );
^

help me please :)

simon
scb
New User
 
Posts: 4
Joined: Thu Oct 25, 2007 12:29 pm

Postby lawrie » Fri Oct 26, 2007 9:23 pm

Use DataOutputStream instead of PrintStream. If you need to write String data to the file, you can use the writeChars method. There are methods for writing most other primitive data types.
lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Postby scb » Fri Oct 26, 2007 9:52 pm

thanks, using

DataOutputStream out = new DataOutputStream( new FileOutputStream("data.txt") );

I obtain

Lego.java:36: cannot find symbol
symbol : constructor FileOutputStream(java.lang.String)
location: class java.io.FileOutputStream
out = new DataOutputStream( new FileOutputStream("data.txt") );
^
1 error

how i can solve the problem?
scb
New User
 
Posts: 4
Joined: Thu Oct 25, 2007 12:29 pm

Postby lawrie » Fri Oct 26, 2007 10:35 pm

You will need to do:


File f = new File("data.txt");
f.createNewFile();
DataOutputStream out = new DataOutputStream( new FileOutputStream(f));
lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Postby scb » Sat Oct 27, 2007 9:29 am

thanks!!
But there is a new problem; in fact using

if(count < 10){
try {
out.writeInt(power);
out.writeChars((new Integer(power).toString()) + pid_val);
out.writeChar(';');
} catch (IOException e) {}
}
else if(count == 10){
try {
out.close();
} catch (IOException e) {}
}
count++;
}

i obtain this output file:

7 5 5 ; 7 5 5 ; 7 5 5 ; 8 5 6 ; 7 5 5 ; 9 5 7 ; @ 6 4 ; < 6 0 ; @ 6 4 ; ? 6 3 ;
mmmm
how i can print also floating point number (Double) and without spaces betwen two digit??
scb
New User
 
Posts: 4
Joined: Thu Oct 25, 2007 12:29 pm

Postby scb » Sat Oct 27, 2007 11:31 am

i used
Code: Select all
   if(count < 10){
            try {
               // out.writeInt(power);
               StringBuffer strPid = new StringBuffer(16);
               StringBuffer strPower = new StringBuffer(8);
               strPid.append(pid_val);
               strPower.append(power);
               out.writeChars(strPower.toString()+","+ strPid.toString() + ";");
               
            } catch (IOException e) {}
         }
         else if(count == 10){
            try {
               out.close();
            } catch (IOException e) {}
         }

:) bye
scb
New User
 
Posts: 4
Joined: Thu Oct 25, 2007 12:29 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff