public String ReadLocalFile() throws IOException { FileConnection fc = null; InputStream is = null; StringBuffer sb = new StringBuffer(); try { // String local_code = ReadLocalCode("expe"); String local_code = "201102"; String url = sFileSchema + sFSRoot + sLocalExpePrefix + local_code + sLocalDataExt; fc = (FileConnection) Connector.open(url, Connector.READ); if (fc == null) { SetMessages("[!] ERROR: fc == null."); System.out.println(GetMessages()); return null; } if (!fc.exists()) { SetMessages("[!] ERROR: !fc.exists()."); System.out.println(GetMessages()); return null; } is = fc.openInputStream(); int ch; while ((ch = is.read()) != -1) { sb.append((char) ch); } // System.out.println(sb.toString()); } catch (ConnectionNotFoundException cnfex) { SetMessages(cnfex.toString()); cnfex.printStackTrace(); } catch (IOException ioex) { SetMessages(ioex.toString()); ioex.printStackTrace(); } catch (Exception ex) { SetMessages(ex.toString()); ex.printStackTrace(); } finally { if (is != null) { is.close(); } if (fc != null) { fc.close(); } } return sb.toString(); }
public String ReadOutputFile() throws IOException { FileConnection fc = null; InputStream is = null; StringBuffer sb = new StringBuffer(); try { String url = sFileSchema + sFSRoot + sLocalOutputPrefix + "201101" + sOutputExt; fc = (FileConnection) Connector.open(url, Connector.READ_WRITE); if (fc == null) { SetMessages("[!] ERROR: fc == null."); System.out.println(GetMessages()); return null; } if (!fc.exists()) { // SetMessages("[!] ERROR: !fc.exists()."); // System.out.println(GetMessages()); fc.create(); return ""; } is = fc.openInputStream(); int ch; while ((ch = is.read()) != -1) { sb.append((char) ch); } } catch (ConnectionNotFoundException cnfex) { SetMessages(cnfex.toString()); cnfex.printStackTrace(); } catch (IOException ioex) { SetMessages(ioex.toString()); ioex.printStackTrace(); } catch (Exception ex) { SetMessages(ex.toString()); ex.printStackTrace(); } finally { if (is != null) { is.close(); } if (fc != null) { fc.close(); } } return sb.toString(); }
public boolean WriteLocalFile(String sWriteData, String sFileURL) throws IOException { FileConnection fc = null; DataOutputStream dos = null; // OutputStream os = null; // PrintStream ps = null; try { // If exists already, first delete file, a little clumsy. // fc = (FileConnection) Connector.open(fileURL.toString(), Connector.READ_WRITE); // String url = sFileSchema + sFSRoot + "expeout_201101" + sFileExt; String url = sFileSchema + sFSRoot + sLocalOutputPrefix + "201102" + sOutputExt; // fc = (FileConnection) Connector.open(url, Connector.READ_WRITE); fc = (FileConnection) Connector.open(sFileURL, Connector.READ_WRITE); // fc = (FileConnection) Connector.open(url, Connector.WRITE); if (!fc.exists()) { fc.create(); } fc.truncate(0); dos = new DataOutputStream(fc.openOutputStream()); // dos.writeUTF(sWriteData); // dos.write("ABCDE".getBytes()); dos.write(sWriteData.getBytes()); // os = fc.openOutputStream(Long.MAX_VALUE); // os = fc.openOutputStream(); // ps = new PrintStream(os); // ps.println(sWriteData); SetSuccess(true); } catch (ConnectionNotFoundException cnfex) { SetSuccess(false); SetMessages(cnfex.toString()); cnfex.printStackTrace(); } catch (IOException ioex) { SetSuccess(false); SetMessages(ioex.toString()); ioex.printStackTrace(); } catch (Exception ex) { SetSuccess(false); SetMessages(ex.toString()); ex.printStackTrace(); } finally { /*if (ps != null) { ps.flush(); ps.close(); } if (os != null) { os.flush(); os.close(); }*/ if (dos != null) { dos.flush(); dos.close(); } if (fc != null) { fc.close(); } } return GetSuccess(); }
public String ReadRemoteFile() throws IOException { HttpConnection hc = null; // DataInputStream dis = null; InputStream is = null; ByteArrayOutputStream baos = null; byte[] data = null; String sResult = ""; StringBuffer sb = new StringBuffer(); int length = 0; try { // String local_code = ReadLocalCode("expe"); String local_code = "201101"; String url = sHttpSchema + sRemoteRoot + sLocalExpePrefix + local_code + sLocalDataExt; hc = (HttpConnection) Connector.open(url); hc.setRequestMethod(HttpConnection.GET); int iResponseCode = hc.getResponseCode(); if (iResponseCode == HttpConnection.HTTP_OK) { is = hc.openInputStream(); length = (int) hc.getLength(); if (length != -1) { data = new byte[length]; is.read(data); sResult = new String(data); } else { baos = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { baos.write(ch); } sResult = new String(baos.toByteArray()); } System.out.println(sResult); } else { sResult = "ERROR: NO iResponseCode == HttpConnection.HTTP_OK"; System.out.println(sResult); } /*if (length != -1) { data = new byte[length]; dis = new DataInputStream(hc.openInputStream()); //dis.readFully(data); } else { SetMessages("ERROR: Content length not given."); }*/ /*int ch; while ((ch = is.read()) != -1) { sb.append((char) ch); }*/ // sResult = sb.toString(); // System.out.println(sResult); } catch (ConnectionNotFoundException cnfex) { SetMessages(cnfex.toString()); sResult = GetMessages(); cnfex.printStackTrace(); } catch (IOException ioex) { SetMessages(ioex.toString()); sResult = GetMessages(); ioex.printStackTrace(); } catch (Exception ex) { SetMessages(ex.toString()); sResult = GetMessages(); ex.printStackTrace(); } finally { if (baos != null) { baos.close(); } if (is != null) { is.close(); } if (hc != null) { hc.close(); } } return sResult; }