matthiasblaesing commented on code in PR #7344:
URL: https://github.com/apache/netbeans/pull/7344#discussion_r1586568018
##########
platform/openide.util.ui/test/unit/src/org/openide/util/test/TestFileUtils.java:
##########
@@ -48,52 +46,48 @@ public class TestFileUtils {
private TestFileUtils() {}
/**
- * Create a new data file with specified initial contents.
+ * Create a new data file with specified initial contents or overwrite
existing file.
* @param f a file to create (parents will be created automatically)
* @param body the complete contents of the new file (in UTF-8 encoding)
*/
public static File writeFile(File f, String body) throws IOException {
f.getParentFile().mkdirs();
- OutputStream os = new FileOutputStream(f);
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,
StandardCharsets.UTF_8));
- pw.print(body);
- pw.flush();
- os.close();
+ Files.write(f.toPath(), body.getBytes(), StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
+ // TODO jdk11
+// Files.writeString(f.toPath(), body, StandardOpenOption.CREATE_NEW);
return f;
}
/**
* Read the contents of a file as a single string.
- * @param a data file
+ * @param file data file
* @return its contents (in UTF-8 encoding)
*/
public static String readFile(File file) throws IOException {
- InputStream is = new FileInputStream(file);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buf = new byte[4096];
- int read;
- while ((read = is.read(buf)) != -1) {
- baos.write(buf, 0, read);
- }
- is.close();
- return baos.toString("UTF-8");
+ return new String(Files.readAllBytes(file.toPath()),
StandardCharsets.UTF_8);
+ // TODO jdk11
+// return Files.readString(file.toPath());
}
/**
* Read the contents of a file as a byte array.
- * @param a data file
+ * @param file data file
* @return its raw binary contents
*/
public static byte[] readFileBin(File file) throws IOException {
- InputStream is = new FileInputStream(file);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buf = new byte[4096];
- int read;
- while ((read = is.read(buf)) != -1) {
- baos.write(buf, 0, read);
+ return Files.readAllBytes(file.toPath());
+ }
+
+ /**
+ * Deletes the file or directory and its contents.
+ */
+ public static void deleteFile(File file) throws IOException {
+ if (file.isDirectory() && file.equals(file.getCanonicalFile())) {
Review Comment:
A thanks - kind of makes sense. Indeed I though, that `File.walk` might make
more sense here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists