List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:medcheck.Medcheck.java
private static String getJSONString(File jsonFile) { String jsonString = ""; try {//www.ja va2s . co m Scanner fileScanner = new Scanner(jsonFile); boolean isFirstLine = true; while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine().trim(); if (!isFirstLine) { jsonString += " "; } isFirstLine = false; jsonString += line; } } catch (Exception e) { System.out.println("ERROR: " + e.getMessage()); e.printStackTrace(); exit(1); } return jsonString; }
From source file:com.thalesgroup.sonar.plugins.tusar.utils.Utils.java
/** * Function that returns the extensions available in file open into the given Scanner * @param scanner//from w w w . ja va2 s . com * @return An array of strings containing the file extensions */ public static String[] getExtensions(Scanner scanner) { List<String> extensions = new ArrayList<String>(); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.startsWith(".")) { line = line.substring(1, line.length()); } if (!line.isEmpty()) { extensions.add(line); } } return extensions.toArray(new String[extensions.size()]); }
From source file:amazonechoapi.AmazonEchoApi.java
private static boolean checkItemId(String itemId) throws IOException { File file = new File("Items.txt"); boolean ret = false; try {//from w w w . j a v a 2 s .com if (!file.exists()) { file.createNewFile(); } Scanner scanner = new Scanner(file); int lineNum = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); lineNum++; if (line.contains(itemId)) { ret = true; } } } catch (Exception e) { ret = false; } return ret; }
From source file:org.exoplatform.commons.notification.template.TemplateResourceBundle.java
static private String getContent(InputStream input) throws IOException { StringBuilder content = new StringBuilder(); Scanner scanner = new Scanner(input, "UTF-8"); try {/*from ww w . ja v a2 s.co m*/ while (scanner.hasNextLine()) { if (content.length() > 0) { content.append("\n"); } String s = scanner.nextLine(); content.append(s); } } finally { scanner.close(); input.close(); } return content.toString(); }
From source file:nl.b3p.applet.local.Shapefiles.java
/** Returns a JSON object with shapefile and DBF metadata. * /*from w ww . jav a 2 s . co m*/ * @param file the shapefile to read * @return a JSON object, read the code to find out which properties */ public static String getMetadata(String file) throws IOException, JSONException { if (!file.toLowerCase().endsWith(".shp")) { throw new IllegalArgumentException("File does not end with .shp: " + file); } FileChannel channel = new FileInputStream(file).getChannel(); ShapefileHeader header = new ShapefileHeader(); ByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); header.read(bb, true); channel.close(); channel = null; file = file.substring(0, file.length() - 4) + ".dbf"; JSONObject j = new JSONObject(); j.put("type", header.getShapeType().name); j.put("version", header.getVersion()); j.put("minX", header.minX()); j.put("minY", header.minY()); j.put("maxX", header.maxX()); j.put("maxY", header.maxY()); JSONObject dbf = new JSONObject(); j.put("dbf", dbf); try { channel = new FileInputStream(file).getChannel(); DbaseFileHeader dheader = new DbaseFileHeader(); bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); dheader.readHeader(bb); dbf.put("numRecords", dheader.getNumRecords()); JSONArray fields = new JSONArray(); dbf.put("fields", fields); for (int i = 0; i < dheader.getNumFields(); i++) { JSONObject field = new JSONObject(); fields.put(field); field.put("name", dheader.getFieldName(i)); field.put("length", dheader.getFieldLength(i)); field.put("decimalCount", dheader.getFieldDecimalCount(i)); field.put("class", dheader.getFieldClass(i).getName().toString()); field.put("type", dheader.getFieldType(i) + ""); } } catch (Exception e) { dbf.put("error", e.toString()); } finally { if (channel != null) { channel.close(); } } file = file.substring(0, file.length() - 4) + ".prj"; File f = new File(file); String prj = null; if (f.exists()) { Scanner s = new Scanner(f); prj = ""; try { while (s.hasNextLine()) { if (prj.length() > 0) { prj += "\n"; } prj += s.nextLine(); } } finally { s.close(); } } j.put("prj", prj); return j.toString(); }
From source file:nl.b3p.catalog.arcgis.Shapefiles.java
/** Returns a JSON object with shapefile and DBF metadata. * * @param file the shapefile to read//from w w w. j a v a 2 s . co m * @return a JSON object, read the code to find out which properties */ public static String getMetadata(String file) throws IOException, JSONException { if (!file.toLowerCase().endsWith(".shp")) { throw new IllegalArgumentException("File does not end with .shp: " + file); } JSONObject j = new JSONObject(); String localFilename = new File(file).getName(); String title = ""; int dotIndex = localFilename.lastIndexOf("."); if (dotIndex > 0) { title = localFilename.substring(0, dotIndex); } else if (dotIndex == 0) { title = localFilename.substring(1); } j.put("title", title); FileChannel channel = new FileInputStream(file).getChannel(); ShapefileHeader header = new ShapefileHeader(); ByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); header.read(bb, true); channel.close(); channel = null; file = file.substring(0, file.length() - 4) + ".dbf"; j.put("type", header.getShapeType().name); j.put("version", header.getVersion()); j.put("minX", header.minX()); j.put("minY", header.minY()); j.put("maxX", header.maxX()); j.put("maxY", header.maxY()); JSONObject dbf = new JSONObject(); j.put("dbf", dbf); try { channel = new FileInputStream(file).getChannel(); DbaseFileHeader dheader = new DbaseFileHeader(); bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); dheader.readHeader(bb); dbf.put("numRecords", dheader.getNumRecords()); JSONArray fields = new JSONArray(); dbf.put("fields", fields); for (int i = 0; i < dheader.getNumFields(); i++) { JSONObject field = new JSONObject(); fields.put(field); field.put("name", dheader.getFieldName(i)); field.put("length", dheader.getFieldLength(i)); field.put("decimalCount", dheader.getFieldDecimalCount(i)); field.put("class", dheader.getFieldClass(i).getName().toString()); field.put("type", dheader.getFieldType(i) + ""); } } catch (Exception e) { dbf.put("error", e.toString()); } finally { if (channel != null) { channel.close(); } } file = file.substring(0, file.length() - 4) + ".prj"; File f = new File(file); String prj = null; if (f.exists()) { Scanner s = new Scanner(f); prj = ""; try { while (s.hasNextLine()) { if (prj.length() > 0) { prj += "\n"; } prj += s.nextLine(); } } finally { s.close(); } } j.put("prj", prj); return j.toString(); }
From source file:io.github.seanboyy.lotReset.json.ReadJSON.java
/**Read JSON file by using configuration file's JSON value, * which points to the file/*from w w w . ja v a2s . co m*/ * @param configLocation String which specifies the location of the config.properties file * @return <code>ArrayList<ArrayList<Lot>> lots</code> if no errors are encountered * @since 1.0 - Implemented config.properties in 2.0 */ public static ArrayList<ArrayList<Lot>> read(String configLocation) { JSONParser parser = new JSONParser(); URL url; File f; //lots is multidimensional in this format: {{fromLot,toLot},{fromLot,toLot},{fromLot,toLot},etc...} ArrayList<ArrayList<Lot>> lots = new ArrayList<ArrayList<Lot>>(); Config config = ReadConfig.read(configLocation); final String[] alphabet = config.getAlpha(); final String[] types = config.getType(); final String[] worlds = config.getWorld(); try { url = new URL(config.getJSON()); InputStream in = url.openStream(); f = File.createTempFile("temp", "json"); FileWriter file = new FileWriter(f); Scanner input = new Scanner(in); while (input.hasNextLine()) { file.write(input.nextLine()); } input.close(); file.close(); Object obj = parser.parse(new FileReader(f)); JSONObject jsonObj = (JSONObject) obj; JSONObject regions = (JSONObject) jsonObj.get("Regions"); for (String a : worlds) { for (String b : types) { for (String c : alphabet) { for (int d = 1; d <= alphabet.length; ++d) { String lotId = a + "-" + b + "-" + c + d; String lotIdA = a + "_" + b + "-" + c + d; String lotIdB = a + "-" + b + "_" + c + d; String lotIdC = a + "_" + b + "_" + c + d; JSONObject lot = (JSONObject) regions.get(lotId); JSONObject lotA = (JSONObject) regions.get(lotIdA); JSONObject lotB = (JSONObject) regions.get(lotIdB); JSONObject lotC = (JSONObject) regions.get(lotIdC); ArrayList<Lot> lotInfo = new ArrayList<Lot>(); if (lot != null) { lotInfo.add(new Lot((long) lot.get("source_minX"), (long) lot.get("source_maxX"), (long) lot.get("source_minY"), (long) lot.get("source_maxY"), (long) lot.get("source_minZ"), (long) lot.get("source_maxZ"), (String) lot.get("source_file"), lotId)); lotInfo.add(new Lot((long) lot.get("dest_minX"), (long) lot.get("dest_maxX"), (long) lot.get("dest_minY"), (long) lot.get("dest_maxY"), (long) lot.get("dest_minZ"), (long) lot.get("dest_maxZ"), (String) lot.get("dest_file"), lotId)); lots.add(lotInfo); } if (lotA != null) { lotInfo.add(new Lot((long) lotA.get("source_minX"), (long) lotA.get("source_maxX"), (long) lotA.get("source_minY"), (long) lotA.get("source_maxY"), (long) lotA.get("source_minZ"), (long) lotA.get("source_maxZ"), (String) lotA.get("source_file"), lotIdA)); lotInfo.add(new Lot((long) lotA.get("dest_minX"), (long) lotA.get("dest_maxX"), (long) lotA.get("dest_minY"), (long) lotA.get("dest_maxY"), (long) lotA.get("dest_minZ"), (long) lotA.get("dest_maxZ"), (String) lotA.get("dest_file"), lotIdA)); lots.add(lotInfo); } if (lotB != null) { lotInfo.add(new Lot((long) lotB.get("source_minX"), (long) lotB.get("source_maxX"), (long) lotB.get("source_minY"), (long) lotB.get("source_maxY"), (long) lotB.get("source_minZ"), (long) lotB.get("source_maxZ"), (String) lotB.get("source_file"), lotIdB)); lotInfo.add(new Lot((long) lotB.get("dest_minX"), (long) lotB.get("dest_maxX"), (long) lotB.get("dest_minY"), (long) lotB.get("dest_maxY"), (long) lotB.get("dest_minZ"), (long) lotB.get("dest_maxZ"), (String) lotB.get("dest_file"), lotIdB)); lots.add(lotInfo); } if (lotC != null) { lotInfo.add(new Lot((long) lotC.get("source_minX"), (long) lotC.get("source_maxX"), (long) lotC.get("source_minY"), (long) lotC.get("source_maxY"), (long) lotC.get("source_minZ"), (long) lotC.get("source_maxZ"), (String) lotC.get("source_file"), lotIdC)); lotInfo.add(new Lot((long) lotC.get("dest_minX"), (long) lotC.get("dest_maxX"), (long) lotC.get("dest_minY"), (long) lotC.get("dest_maxY"), (long) lotC.get("dest_minZ"), (long) lotC.get("dest_maxZ"), (String) lotC.get("dest_file"), lotIdC)); lots.add(lotInfo); } } } } } } catch (IOException e) { System.out.println("FILE ERROR FROM READING JSON"); e.printStackTrace(); return null; } catch (ParseException e) { System.out.println("PARSER ERROR FROM READING JSON"); e.printStackTrace(); return null; } return lots; }
From source file:com.ExtendedAlpha.SWI.SeparatorLib.InventorySeparator.java
public static ItemStack[] getInventory(File jsonFile, int size) { String source = ""; try {/*from w w w .j av a 2 s .c o m*/ Scanner x = new Scanner(jsonFile); while (x.hasNextLine()) { source += x.nextLine() + "\n"; } x.close(); return getInventory(source, size); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } }
From source file:org.springframework.hateoas.alps.JacksonSerializationTests.java
private static String read(Resource resource) throws IOException { Scanner scanner = null; try {/* w w w . jav a 2 s. c o m*/ scanner = new Scanner(resource.getInputStream()); StringBuilder builder = new StringBuilder(); while (scanner.hasNextLine()) { builder.append(scanner.nextLine()); if (scanner.hasNextLine()) { builder.append("\n"); } } return builder.toString(); } finally { if (scanner != null) { scanner.close(); } } }
From source file:org.apache.streams.lucene.TestLucenSimpleTaggingProcessor.java
@BeforeClass public static void setUpTags() { tags = Lists.newLinkedList();/*from w ww . j av a 2 s . c o m*/ Scanner scanner = new Scanner(TestLucenSimpleTaggingProcessor.class.getResourceAsStream("/TestTags.tsv")); while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split("\t"); tags.add(new LanguageTag(line[0], line[1], "en")); } mapper = new ObjectMapper(); }