Difference between revisions of "Java: RoboRIO Files"

From Deep Blue Robotics Wiki
Jump to: navigation, search
Line 11: Line 11:
  
 
Here is some example code to write a file with 3 lines of text:
 
Here is some example code to write a file with 3 lines of text:
 
+
<syntax highlight="java">
 
  try {
 
  try {
 
     FileWriter fileWriter = new FileWriter("/home/lvuser/Preferences.txt");
 
     FileWriter fileWriter = new FileWriter("/home/lvuser/Preferences.txt");
Line 25: Line 25:
 
     e.printStackTrace();
 
     e.printStackTrace();
 
  }
 
  }
 
+
</syntaxhighlight>
  
 
File Reading
 
File Reading

Revision as of 22:37, 1 December 2016

This page describes how to read and write from files stored on the RoboRIO. So far we have used this process primarily for storing robot preferences. This topic will be much easier to understand if you have already learned about I/O Streams.

The only directory of the RoboRIO that can be edited by the program is /home/lvuser. File operations in this directory can be performed the same way as on a computer. Remember that writing to an existing file will erase all of its previous content. If you do not already know how to read and write files, then you can follow these steps:

File Writing

  1. Create a new FileWriter object (all of these classes will need to be imported from java.io). The parameter for the constructor is the file path, in this case "/home/lvuser/<FileName>.txt". All of these steps will need to occur inside of a try-catch statement for IOExceptions.
  2. Create a new BufferedWriter object. The parameter for the constructor is the FileWriter that you just created.
  3. Use BufferedWriter's write and newLine methods to write the text of the file.
  4. After you finish writing the file, call the flush and the close method to close the file.
  5. Here is some example code to write a file with 3 lines of text: <syntax highlight="java">

    try {
       FileWriter fileWriter = new FileWriter("/home/lvuser/Preferences.txt");
       BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
       bufferedWriter.write("Line 1");
       bufferedWriter.newLine();
       bufferedWriter.write("Line 2");
       bufferedWriter.newLine();
       bufferedWriter.write("Line 3");
       bufferedWriter.flush();
       bufferedWriter.close();
    } catch (IOException e) {
       e.printStackTrace();
    }
    

    </syntaxhighlight>

    File Reading

    1. Create a new FileReader object (all of these classes will need to be imported from java.io). The parameter for the constructor is the file path, in this case "/home/lvuser/<FileName>.txt". All of these steps will need to occur inside of a try-catch statement for IOExceptions.
    2. Create a new BufferedReader object. The parameter for the constructor is the FileReader that you just created.
    3. Use the readLine method to read the text of the file one line at a time. The method will return null once it reaches the end of the file.
    4. After you finish reading the file, call the close method to close the file.
    5. Here is some example code to print out the contents of a file:

        try { 
          FileReader fileReader = new FileReader("/home/lvuser/Preferences.txt");
          BufferedReader bufferedReader = new BufferedReader(fileReader); 
          for(int i=0; i<MAX_FILE_SIZE; i++) {
              String line = bufferedReader.readLine();
              if (line == null) {
                  break; 
              } else {
                  System.out.println(line);
              }
          } 
          bufferedReader.close(); 
       } catch (IOException e) { 
          e.printStackTrace(); 
       }