Does anyone know how to write text files to storage on the roborio with java?
If so, what directories do I have access to?
Here’s my code if it helps:
package org.usfirst.frc.team1559.robot;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import edu.wpi.first.wpilibj.IterativeRobot;
public class Robot extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
File f;
BufferedWriter bw;
FileWriter fw;
public void robotInit() {
try {
f = new File("~/Output.txt");
if(!f.exists()){
f.createNewFile();
}
fw = new FileWriter(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bw = new BufferedWriter(fw);
}
public void teleopInit(){
try {
bw.write("Hellow, I'm a text file");
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
With this code I get the following error:
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at org.usfirst.frc.team1559.robot.Robot.robotInit(Robot.java:32)
at edu.wpi.first.wpilibj.IterativeRobot.startCompetition(IterativeRobot.java:76)
at edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:234)
ERROR Unhandled exception: java.lang.NullPointerException at [java.io.Writer.<init>(Writer.java:88), java.io.BufferedWriter.<init>(BufferedWriter.java:101), java.io.BufferedWriter.<init>(BufferedWriter.java:88), org.usfirst.frc.team1559.robot.Robot.robotInit(Robot.java:39), edu.wpi.first.wpilibj.IterativeRobot.startCompetition(IterativeRobot.java:76), edu.wpi.first.wpilibj.RobotBase.main(RobotBase.java:234)]
WARNING: Robots don’t quit!
—> The startCompetition() method (or methods called by it) should have handled the exception above.
The ~ is normally interpreted by the shell, not the kernel. I believe that java is attempting to open the file directly through an fopen() or equivalent call. I believe that you can get the value of the HOME environment variable and use it in place of the ~ to avoid hardcoding paths.
.
.
Map<String, String> env = System.getenv();
.
.
f = new File(env.get("HOME") + "/Output.txt");
.
.
Hi All -
I have looked around the links on this post for more information about using a USB stick, but can’t find much.
I see that I can reference /U/ and /V/ (presumably for the two USB ports).
When I plug a Kingston NTFS-formatted USB stick and connect via FTP (Filezilla) I can see the KINGSTON device, but not the folders. I assume this is because the stick is formatted NTFS.
If I use a FAT-formatted USB stick, will I see it as /U or /V depending on the port it is plugged into? Any other fine points about getting this to work for data logging?
Sorry forgot to mention that I did look for /media/sda1, and I do not see the device listed that way. Neither do I see it listed as U or V.
I have since formatted my device as FAT and tried again with no luck. It is a 64Gb stick - any chance there is a size limit?
As an alternative I could use the internal storage. Read in the specs for the Roborio that the internal storage capacity is 256 MB. Can anyone tell what the approx remaining capacity is after loading JRE and robot program? Filezilla is not help me figure that out.
FAT was traditionally limited to 4Gb. It seems to have been extended a bit. However, a recent search turned up:
You cannot decrease the cluster size on a volume using the FAT32 file system so that the FAT ends up larger than 16 MB less 64 KB in size.
It sounds like Micros*** is utterly muddying the waters yet again; this is worse than traditional by several orders of magnitude.
They also included on the same page, probably more germane:
You cannot format a volume larger than 32 GB in size using the FAT32 file system in Windows 2000. The Windows 2000 FastFAT driver can mount and support volumes larger than 32 GB that use the FAT32 file system (subject to the other limits), but you cannot create one using the Format tool. This behavior is by design. If you need to create a volume larger than 32 GB, use the NTFS file system instead.
Another option is simply defining a non-absolute path, which would make the file relative to the working directory (where the jar file is located).
File file = new File("Output.txt");
Other than that, referring to solving “File or directory not found,” you also have to create the directory that the desired file resides in, in addition to the actual file.
File file = new File("new_directory/Output.txt");
file.mkdirs();
file.createNewFile();
This would ensure that the directory “new_directory” is created before “Output.txt” is created.