Go to Post I suspect the GDC is enjoying watching teams wrap their heads around this ranking algorithm.... and is probably a bit surprised that it has taken teams so long to clue in to the options this ranking system presents to them. - dtengineering [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 19-01-2014, 17:47
blanketboyjo blanketboyjo is offline
Registered User
FRC #0687
 
Join Date: Jan 2014
Location: Southern California
Posts: 2
blanketboyjo is an unknown quantity at this point
File IO without overwriting file

Hi, so I am trying to make the CRIO print out to a text file that I can read through ftp. I have it working to make the file, but everytime the code runs it overwrites the file, is there any way to make it so that new lines are added to the existing file?
Code:
PrintStream writer;
DataOutputStream myFile;
FileConnection con;

try {
con = (FileConnection)Connector.open("file:///test.txt", Connector.WRITE);
con.create();
myFile = con.openDataOutputStream();
writer = new PrintStream(myFile);
} catch (Exception e) {
}
writer.println("Message");

Last edited by blanketboyjo : 19-01-2014 at 17:47. Reason: snipped code
Reply With Quote
  #2   Spotlight this post!  
Unread 20-01-2014, 08:03
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,728
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: File IO without overwriting file

Try

writer.append("Message");
Reply With Quote
  #3   Spotlight this post!  
Unread 21-01-2014, 00:58
blanketboyjo blanketboyjo is offline
Registered User
FRC #0687
 
Join Date: Jan 2014
Location: Southern California
Posts: 2
blanketboyjo is an unknown quantity at this point
Re: File IO without overwriting file

I tried that but it says that append() is not a function of dataoutputstream. Is there anything else I can do?
Reply With Quote
  #4   Spotlight this post!  
Unread 21-01-2014, 11:44
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: File IO without overwriting file

What if you tried something like this?

Code:
public void appendToFile() {
    FileConnection file;
    OutputStream out;
    PrintStream writer;

    try {
        // Open file for writing
        file = (FileConnection) Connector.open("file:///test.txt", Connector.WRITE);
        out = file.openOutputStream(Long.MAX_VALUE);
        writer = new PrintStream(out);

        // Append data to file
        writer.println("Message");

        // Close file connection
        writer.close();
        out.close();
        file.close();
    } catch (IOException ex) {
        // Handle exception
        ex.printStackTrace();
    }
}
The parameter passed to openOutputStream() specifies the offset position for writing to the file. Since the offset (should be) larger than the file size, it moves it to the end of the file, allowing you to append to the file instead of overwriting it.

Source: http://myossdevblog.blogspot.com/200...a-me-j2me.html
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)

Reply With Quote
  #5   Spotlight this post!  
Unread 25-01-2014, 19:06
JohnGilb JohnGilb is offline
Programming Mentor, Drive Mentor
FRC #0488
 
Join Date: Mar 2011
Rookie Year: 2003
Location: Redmond, WA
Posts: 116
JohnGilb has a spectacular aura aboutJohnGilb has a spectacular aura aboutJohnGilb has a spectacular aura about
Re: File IO without overwriting file

I've been getting IO exceptions whenever I try anything along the lines of

OutputStreamWriter writer = new OutputStreamWriter(connection.openOutputStream(Lon g.MAX_VALUE));


or index out of bounds exceptions when I try:

writer.write(entry, (int)connection.fileSize(), entry.length))

(notably, for a file with about 15 characters in it, fileSize is returning 4831838208. That doesn't seem healthy or right.)

Has anybody been able to append to a file successfully on the robot?
Reply With Quote
  #6   Spotlight this post!  
Unread 25-01-2014, 20:13
NotInControl NotInControl is offline
Controls Engineer
AKA: Kevin
FRC #2168 (Aluminum Falcons)
Team Role: Engineer
 
Join Date: Oct 2011
Rookie Year: 2004
Location: Groton, CT
Posts: 261
NotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond repute
Re: File IO without overwriting file

Quote:
Originally Posted by blanketboyjo View Post
Code:
PrintStream writer;
DataOutputStream myFile;
FileConnection con;

try {
con = (FileConnection)Connector.open("file:///test.txt", Connector.WRITE);
use Connector.Read_Write

try something like this

Code:
Connection conn = Connector.open(String fileName, Connector.READ_WRITE);
FileConnection fc = (FileConnection) conn;
if (!fc.exists()) {
fc.create();
}
OutputStream os = fc.openOutputStream(fc.fileSize());
After opening the file with the above code snippet if you are going to write something to the file then it will append at the end of the file content


Hope this helps,
Kevin
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner
Reply With Quote
  #7   Spotlight this post!  
Unread 27-01-2014, 01:42
JohnGilb JohnGilb is offline
Programming Mentor, Drive Mentor
FRC #0488
 
Join Date: Mar 2011
Rookie Year: 2003
Location: Redmond, WA
Posts: 116
JohnGilb has a spectacular aura aboutJohnGilb has a spectacular aura aboutJohnGilb has a spectacular aura about
Re: File IO without overwriting file

Yeah, that's code that I would very much expect to work.

I am opening the fileconnection using

Code:
FileConnection c = (FileConnection) Connector.open(PATH, Connector.READ_WRITE);
and I have tried
Code:
OutputStreamWriter writer = new OutputStreamWriter(c.openOutputStream(c.fileSize()));
but that triggers an IO exception.

Have you made that code work on the cRIO? I have similar code that works on a PC, but something about the cRIO seems to be causing a problem...
Reply With Quote
  #8   Spotlight this post!  
Unread 27-01-2014, 03:46
NotInControl NotInControl is offline
Controls Engineer
AKA: Kevin
FRC #2168 (Aluminum Falcons)
Team Role: Engineer
 
Join Date: Oct 2011
Rookie Year: 2004
Location: Groton, CT
Posts: 261
NotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond repute
Re: File IO without overwriting file

Yes, what is the need to use the OutputStreamWriter class vs outputstream as in the example shown?

Do you know which IO exception is being thrown? Maybe we can track down the problem that way

simplistic approach:

Code:
try {
  ...
  throw new FileNotFoundException("This is an error message");
  ...
} catch (FileNotFoundException e) {
  System.out.println("File not found: " + e.getMessage());
  ...
} catch (EOFException e) {
  System.out.println("End of file reached: " + e.getMessage());
  ...
} catch (IOException e) { // catch all IOExceptions not handled by previous catch blocks
  System.out.println("General I/O exception: " + e.getMessage());
  e.printStackTrace();
  ...
}
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner
Reply With Quote
  #9   Spotlight this post!  
Unread 27-01-2014, 11:45
joelg236 joelg236 is offline
4334 Retired Mentor & Alumni
AKA: Joel Gallant
no team
Team Role: Mentor
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 733
joelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond repute
Re: File IO without overwriting file

https://github.com/Team4334/atalibj/...TextFiles.java

I've tried dealing with this problem multiple times. Ended in just hacking a solution together.

If anyone's solutions end up working on a cRIO (and after reboots too!) - please let me know or submit a pull request. That'd be awesome.
__________________
All opinions are my own.
Reply With Quote
  #10   Spotlight this post!  
Unread 27-01-2014, 11:54
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,827
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: File IO without overwriting file

What about creating a new log file each time? That's how things are handled with the server production systems I work with - each log file has a timestamp as part of the file name to help prevent overwrites.

I'm not sure how the clock works on the cRIO or what level of access we have towards getting a standardized timestamp, but there are other options as well. For example, you can have each log file generated with an incrementing numerical value (1.log, 2.log, etc). Keep the current value in a config file (log.config). When the program starts up, it reads the config file, increments by 1, writes the new value out to the config file (overwriting the old value) and creates the log file using that value.

This has the added benefit of giving you a separate log file for each run, which will help you keep everything organized.
__________________
2007 - Present: Mentor, 2177 The Robettes
LRI: North Star 2012-2016; Lake Superior 2013-2014; MN State Tournament 2013-2014, 2016; Galileo 2016; Iowa 2017
2015: North Star Regional Volunteer of the Year
2016: Lake Superior WFFA
Reply With Quote
  #11   Spotlight this post!  
Unread 27-01-2014, 13:26
NotInControl NotInControl is offline
Controls Engineer
AKA: Kevin
FRC #2168 (Aluminum Falcons)
Team Role: Engineer
 
Join Date: Oct 2011
Rookie Year: 2004
Location: Groton, CT
Posts: 261
NotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond reputeNotInControl has a reputation beyond repute
Re: File IO without overwriting file

I don't want to get off topic but,

Quote:
Originally Posted by Jon Stratis View Post
I'm not sure how the clock works on the cRIO or what level of access we have towards getting a standardized timestamp, but... .
Unfortunately, the clock gets reset to Jan,1 1970 everytime the cRIO is started.

In the past I have played with reading system time from the Driverstation system clock and sending that over to the cRIO through the dashboard.

In Java there is no way to overwrite the system clock on the cRIO because the JRE doesn't allow access to the clock, you could do it with a JNI system call but I never went that far.

If you don't care about updating the system time on the crio, you could simply get a timestamp from the driverstation on robot startup and keep it in a variable and use that for time stamps, and use System time as a couter to count time since your "time sync".

Hope this helps,
Kevin
__________________
Controls Engineer, Team 2168 - The Aluminum Falcons
[2016 Season] - World Championship Controls Award, District Controls Award, 3rd BlueBanner
-World Championship- #45 seed in Quals, World Championship Innovation in Controls Award - Curie
-NE Championship- #26 seed in Quals, winner(195,125,2168)
[2015 Season] - NE Championship Controls Award, 2nd Blue Banner
-NE Championship- #26 seed in Quals, NE Championship Innovation in Controls Award
-MA District Event- #17 seed in Quals, Winner(2168,3718,3146)
[2014 Season] - NE Championship Controls Award & Semi-finalists, District Controls Award, Creativity Award, & Finalists
-NE Championship- #36 seed in Quals, SemiFinalist(228,2168,3525), NE Championship Innovation in Controls Award
-RI District Event- #7 seed in Quals, Finalist(1519,2168,5163), Innovation in Controls Award
-Groton District Event- #9 seed in Quals, QuarterFinalist(2168, 125, 5112), Creativity Award
[2013 Season] - WPI Regional Winner - 1st Blue Banner
Reply With Quote
  #12   Spotlight this post!  
Unread 27-01-2014, 13:41
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,590
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: File IO without overwriting file

Quote:
Originally Posted by NotInControl View Post
Unfortunately, the clock gets reset to Jan,1 1970 everytime the cRIO is started.
The 8-slot cRIO has a battery backup for the RTC, but the 4-slot does not. See https://decibel.ni.com/content/message/33390#33390.
Reply With Quote
  #13   Spotlight this post!  
Unread 28-01-2014, 13:20
JohnGilb JohnGilb is offline
Programming Mentor, Drive Mentor
FRC #0488
 
Join Date: Mar 2011
Rookie Year: 2003
Location: Redmond, WA
Posts: 116
JohnGilb has a spectacular aura aboutJohnGilb has a spectacular aura aboutJohnGilb has a spectacular aura about
Re: File IO without overwriting file

If we can't get a generic append function working, we'll probably go with multiple incrementing log files, but there are some downsides:

-Since the robot can terminate unexpectedly, we have to flush our buffer fairly often to make sure we don't lose data due to a power failure or unexpected reboot
-Too many small files is a pain to read and process, so we would need to find a balance

I'll try to get more detail about our current IO exception (also, does anybody else have that problem where FileConnection.fileSize() returns an enormous number?), and if that doesn't work out, implement a solution like joelg236's. We already had some code that buffered about 7.9k worth of logs before flushing to disk, so we shouldn't be spending too much time on IO.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:11.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi