/** Closes the file, disabling use of it. */ public void close() { try { m_reader.close(); m_fc.close(); } catch (IOException e) { System.out.println(e.getMessage()); } }
/** * Opens the specified file and prepares it for reading data from. * * @param sFileName */ public FileReader(String sFileName) { m_sFile = sFileName; try { m_fc = (FileConnection) Connector.open(m_sFile, Connector.READ); m_reader = new DataInputStream(m_fc.openInputStream()); } catch (IOException e) { System.out.println(e.getMessage()); } }
public static void logException(Exception ex) { try { ex.printStackTrace(); FileConnection log = (FileConnection) Connector.open("file://" + System.currentTimeMillis() + ".txt"); log.create(); OutputStream logstream = log.openOutputStream(); PrintStream logger = new PrintStream(logstream); logger.println(ex.getMessage()); logger.println(ex.toString()); logger.close(); logstream.close(); } catch (Exception ex2) { ex2.printStackTrace(); } }
// WRITE AS LITTLE AS POSSIBLE, CONSTANT WRITING(every tick) IS BAD FOR CRIO private void write(String path, byte[] data) { if (path == null) { path = DEFAULT_FILE_PATH; } synchronized (FileLock) { FileLock.notifyAll(); } FileConnection File = null; try { File = (FileConnection) Connector.open(path, Connector.READ_WRITE); if (File.exists()) { File.delete(); } File.create(); OutputStream output = File.openOutputStream(); if (data != null) { output.write(data); System.out.println("WRITING"); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (File != null) { try { File.close(); System.out.println("WRITING COMPLETED"); } catch (IOException ex) { } } } }
private String read(String path) { class EndOfStreamException extends Exception {} class Reader { InputStream stream; Reader(InputStream stream) { this.stream = stream; } public void closeStream() { try { stream.close(); } catch (IOException ex) { ex.printStackTrace(); } } public char read() throws IOException, EndOfStreamException { int input = stream.read(); if (input == -1) { return " ".charAt(0); // throw new EndOfStreamException(); } else { // Check for carriage returns return input == '\r' ? '\n' : (char) input; } } char readWithoutWhitespace() throws IOException, EndOfStreamException { while (true) { char value = read(); switch (value) { case ' ': case '\t': continue; default: return value; } } } } System.out.println("Searching on " + path); if (path == null) { path = DEFAULT_FILE_PATH; System.out.println("Path is null"); } synchronized (FileLock) { FileLock.notifyAll(); } String result = null; FileConnection File; try { File = (FileConnection) Connector.open(path, Connector.READ); StringBuffer buffer = new StringBuffer(); System.out.println("File Exists? " + File.exists()); if (File.exists()) { Reader reader = new Reader(File.openInputStream()); while (true) { char value = reader.readWithoutWhitespace(); for (; value != '\n'; value = reader.read()) { if (value == ')' || value == '') { continue; } buffer.append(value); } result = buffer.toString(); reader.closeStream(); break; } } } catch (IOException ex) { ex.printStackTrace(); } catch (EndOfStreamException ex) { ex.printStackTrace(); System.out.println("FINISHED READING"); } return result; }