Java 1.7 Snippets

From PeformIQ Upgrade
Revision as of 17:47, 14 January 2016 by PeterHarding (talk | contribs) (Created page with " =Using an Iterator to Read a File= <pre> try { lines = Files.readAllLines(Paths.get("Request.xml"), StandardCharsets.UTF_8); } catch (Exception e) { System.out.println("Co...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Using an Iterator to Read a File

try
{
	lines = Files.readAllLines(Paths.get("Request.xml"), StandardCharsets.UTF_8);
}
catch (Exception e)
{
	System.out.println("Could not read XML file: " + e.toString());
	lr.abort();
}

if (lines != null)
{
	for (Iterator<String> i = lines.iterator(); i.hasNext(); ) {
		String item = i.next();
		System.out.println(item);
	}

	// or 

	for (String line : lines)
	{
		System.out.println(line);
	}
}