List of usage examples for java.io FileReader read
public int read(java.nio.CharBuffer target) throws IOException
From source file:kjscompiler.Settings.java
public Settings(String path) throws ParseException, FileNotFoundException, IOException { JSONParser parser = new JSONParser(); File file = new File(path); String parent = file.exists() ? file.getParent() : null; File dir = new File(null == parent || parent.isEmpty() ? "." : parent); FileReader fr = new FileReader(file); char[] fileData = new char[(int) file.length()]; fr.read(fileData); String content = new String(fileData); JSONObject obj = (JSONObject) parser.parse(content); if (obj.get("basedir") instanceof JSONArray) { this.baseDir = (JSONArray) obj.get("basedir"); } else {// w ww. ja v a 2s . c o m JSONArray arr = new JSONArray(); arr.add(obj.get("basedir")); this.baseDir = arr; } this.output = (String) obj.get("output"); this.level = (String) obj.get("level"); this.pattern = (String) obj.get("pattern"); this.wrapper = obj.get("wrapper") != null ? (String) obj.get("wrapper") : ""; this.projectPath = dir.getCanonicalPath(); dir = new File(this.projectPath, this.output); this.output = dir.getCanonicalPath(); fr.close(); }
From source file:tiled.plugins.json.JSONMapWriter.java
/** * this method write a map OR a tileset in json format * * @param map/*from w w w . ja v a 2 s.com*/ * @param set * @param filename * @throws Exception */ private void writeMapOrTileset(Map map, TileSet set, String filename) throws Exception { // Create a temporary file File tempFile = File.createTempFile("tiled_json_", ".tmx"); // Write in this temp file an xml content (tmx format) if (map != null) { super.writeMap(map, tempFile.getAbsolutePath()); } else { if (set != null) { super.writeTileset(set, tempFile.getAbsolutePath()); //write in this temp file an xml content (tmx format) } else return; } tempFile = new File(tempFile.getAbsolutePath()); //TODO useful? // Now read this temp file and get the tmx content int fileSize = 100000; //TODO Replace here by true file size //(int) tempFile.length(); //100000; char[] TMXContent = new char[fileSize]; FileReader fileR = new FileReader(tempFile.getAbsolutePath()); fileR.read(TMXContent); fileR.close(); // Avoid retrieving xml header like <?xml version=\"1.0\"?>, JSON parser doesn't like it! String TMXContentString = new String(TMXContent).trim().replaceFirst("\\<\\?.*\\?\\>", ""); System.out.println("temp file path=" + tempFile.getAbsolutePath()); System.out.println("filesize=" + fileSize); System.out.println("content=" + TMXContentString); // Delete useless temp file tempFile.delete(); // Turn it into JSON format string String JSONContent = XML.toJSONObject(TMXContentString).toString(2); System.err.println("json content=" + JSONContent); // Write in destination file FileWriter fileW = new FileWriter(filename); fileW.write(JSONContent); fileW.flush(); fileW.close(); }
From source file:com.hiqes.android.demopermissionsm.ui.ProgLogFragment.java
public void doLoadFile(String filePath) { String errMsg;//from w ww.j a va 2 s . com Context ctx = getActivity(); try { File inFile = new File(filePath); FileReader reader = new FileReader(inFile); char[] text = new char[(int) inFile.length()]; int readCount = reader.read(text); if (readCount != inFile.length()) { errMsg = ctx.getString(R.string.warn_file_truncated); Toast.makeText(getActivity(), R.string.warn_file_truncated, Toast.LENGTH_LONG).show(); Logger.w(TAG, errMsg); } Logger.d(TAG, "Read contents from log: " + inFile.getName()); reader.close(); mLoadedLog.setText(text, 0, readCount); } catch (FileNotFoundException e) { Logger.e(TAG, "Unable to open file: " + filePath); errMsg = ctx.getString(R.string.err_file_not_found); Toast.makeText(getActivity(), errMsg, Toast.LENGTH_LONG).show(); } catch (IOException e) { Logger.e(TAG, "Failed to read saved file: " + e.getMessage()); errMsg = ctx.getString(R.string.err_file_read); Toast.makeText(getActivity(), errMsg, Toast.LENGTH_LONG).show(); } }
From source file:net.ytbolg.mcxa.MCLaucherXA.java
static String ReadFile(String path) throws FileNotFoundException, IOException { File file = new File(path); FileReader r = new FileReader(file); char c[] = new char[(int) file.length()]; r.read(c); return String.valueOf(c); }
From source file:com.bt.aloha.testing.SimpleSipStackLogEnhancerTest.java
private String readTargetFile() throws IOException { FileReader fr = null; try {/*from w w w . j av a 2s. c o m*/ char[] buff = new char[(int) tempTargetFile.length()]; fr = new FileReader(tempTargetFile); fr.read(buff); String res = new String(buff); log.info("Read:\n" + res); return res; } finally { if (fr != null) fr.close(); } }
From source file:org.apache.torque.generator.control.DeeplyNestedMergepointsTest.java
@Test public void testDeeplyNestedMergepointsGeneration() throws Exception { File targetDir = new File("target/test/deeplyNestedMergepoints"); FileUtils.deleteDirectory(targetDir); Controller controller = new Controller(); List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>(); CustomProjectPaths projectPaths = new CustomProjectPaths( new Maven2DirectoryProjectPaths(new File("src/test/deeplyNestedMergepoints"))); projectPaths.setOutputDirectory(null, targetDir); unitDescriptors.add(new UnitDescriptor(UnitDescriptor.Packaging.DIRECTORY, projectPaths, new DefaultTorqueGeneratorPaths())); controller.run(unitDescriptors);/*www .j av a 2s. c om*/ assertTrue(targetDir.exists()); File targetFile = new File(targetDir, "output.txt"); assertTrue(targetFile.exists()); FileReader fileReader = new FileReader(targetFile); StringBuilder content = new StringBuilder(); int readChars; char[] buffer = new char[50]; do { readChars = fileReader.read(buffer); if (readChars != -1) { content.append(buffer, 0, readChars); } } while (readChars != -1); fileReader.close(); assertEquals("content", content.toString()); }
From source file:org.apache.sysml.utils.GenerateClassesForMLContext.java
/** * Obtain the content of a file as a string. * /* w w w . j a va 2 s.c o m*/ * @param filePath * the path to a file * @return the file content as a string */ public static String fileToString(String filePath) { try { File f = new File(filePath); FileReader fr = new FileReader(f); StringBuilder sb = new StringBuilder(); int n; char[] charArray = new char[1024]; while ((n = fr.read(charArray)) > 0) { sb.append(charArray, 0, n); } fr.close(); return sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:main.server.SourceBrowserDataProvider.java
private String readFile(File file) throws IOException { StringBuffer buf = new StringBuffer(); char[] a = new char[4096]; FileReader r = null; try {//from w w w . j a va2s . co m r = new FileReader(file); for (;;) { int sz = r.read(a); if (sz < 0) { break; } buf.append(a, 0, sz); } } finally { if (r != null) { try { r.close(); } catch (IOException e) { // ignore } } } return buf.toString(); }
From source file:com.npower.unicom.sync.FileSyncItemWriter.java
public void close() throws IOException { this.writer.flush(); this.writer.close(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); Writer out = new FileWriter(this.file); out.write("0001"); out.write('\t'); out.write("00000"); out.write('\t'); out.write(format.format(new Date())); out.write('\t'); out.write("ZB"); out.write('\t'); String from = (fromDate != null) ? format.format(fromDate) : "19700101000000"; String to = (toDate != null) ? format.format(toDate) : "19700101000000"; out.write(from);/*from w w w . ja v a 2 s . c o m*/ out.write('\t'); out.write(to); out.write('\t'); out.write(Integer.toString(this.totalRecords)); out.write('\t'); out.write('\n'); // FileReader in = new FileReader(this.bodyFile); char[] buf = new char[512]; int len = in.read(buf); while (len > 0) { out.write(buf, 0, len); out.flush(); len = in.read(buf); } in.close(); out.close(); }
From source file:org.apache.cassandra.tools.SSTableExportTest.java
@Test public void testEnumeratekeys() throws IOException { File tempSS = createTemporarySSTable("Keyspace1", "Standard1"); ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "Standard1"); IPartitioner<?> partitioner = DatabaseDescriptor.getPartitioner(); DataOutputBuffer dob = new DataOutputBuffer(); SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2, partitioner); // Add rowA//ww w . j a v a2 s .c o m cfamily.addColumn(new QueryPath("Standard1", null, "colA".getBytes()), "valA".getBytes(), 1, false); ColumnFamily.serializer().serializeWithIndexes(cfamily, dob); writer.append(partitioner.decorateKey("rowA"), dob); dob.reset(); cfamily.clear(); // Add rowB cfamily.addColumn(new QueryPath("Standard1", null, "colB".getBytes()), "valB".getBytes(), 1, false); ColumnFamily.serializer().serializeWithIndexes(cfamily, dob); writer.append(partitioner.decorateKey("rowB"), dob); dob.reset(); cfamily.clear(); writer.closeAndOpenReader(); // Enumerate and verify File temp = File.createTempFile("Standard1", ".txt"); SSTableExport.enumeratekeys(writer.getFilename(), new PrintStream(temp.getPath())); FileReader file = new FileReader(temp); char[] buf = new char[(int) temp.length()]; file.read(buf); String output = new String(buf); String sep = System.getProperty("line.separator"); assert output.equals("rowA" + sep + "rowB" + sep) : output; }