test/testlibrary/com/oracle/java/testlibrary/Utils.java

Print this page

        

*** 30,46 **** --- 30,50 ---- import java.io.IOException; import java.lang.reflect.Field; import java.net.InetAddress; import java.net.ServerSocket; import java.net.UnknownHostException; + import java.nio.file.Files; + import java.nio.file.Path; + import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; + import java.util.stream.Collectors; import sun.misc.Unsafe; /** * Common library for various test helper functions. */
*** 316,347 **** /** * Return the contents of the named file as a single String, * or null if not found. * @param filename name of the file to read * @return String contents of file, or null if file not found. */ ! public static String fileAsString(String filename) { ! StringBuilder result = new StringBuilder(); ! try { ! File file = new File(filename); ! if (file.exists()) { ! BufferedReader reader = new BufferedReader(new FileReader(file)); ! while (true) { ! String line = reader.readLine(); ! if (line == null) { ! break; ! } ! result.append(line).append("\n"); ! } ! } else { ! // Does not exist: ! return null; ! } ! } catch (Exception e) { ! e.printStackTrace(); ! } ! return result.toString(); } /** * @return Unsafe instance. */ --- 320,338 ---- /** * Return the contents of the named file as a single String, * or null if not found. * @param filename name of the file to read * @return String contents of file, or null if file not found. + * @throws IOException + * if an I/O error occurs reading from the file or a malformed or + * unmappable byte sequence is read */ ! public static String fileAsString(String filename) throws IOException { ! Path filePath = Paths.get(filename); ! return Files.exists(filePath) ! ? Files.lines(filePath).collect(Collectors.joining(NEW_LINE)) ! : null; } /** * @return Unsafe instance. */