mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2024-12-14 22:30:33 +05:30
commit
2f02c0e6a4
79
extractor/src/test/java/org/schabi/newpipe/FileUtils.java
Normal file
79
extractor/src/test/java/org/schabi/newpipe/FileUtils.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package org.schabi.newpipe;
|
||||||
|
|
||||||
|
import com.grack.nanojson.JsonArray;
|
||||||
|
import com.grack.nanojson.JsonObject;
|
||||||
|
import com.grack.nanojson.JsonWriter;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Util class to write file to disk
|
||||||
|
* <p>
|
||||||
|
* Can be used to debug and test, for example writing a service's JSON response
|
||||||
|
* (especially useful if the response provided by the service is not documented)
|
||||||
|
*/
|
||||||
|
public class FileUtils {
|
||||||
|
|
||||||
|
public static void createFile(String path, JsonObject content) throws IOException {
|
||||||
|
createFile(path, jsonObjToString(content));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createFile(String path, JsonArray array) throws IOException {
|
||||||
|
createFile(path, jsonArrayToString(array));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a file given a path and its content. Create subdirectories if needed
|
||||||
|
*
|
||||||
|
* @param path the path to write the file, including the filename (and its extension)
|
||||||
|
* @param content the content to write
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static void createFile(final String path, final String content) throws IOException {
|
||||||
|
final String[] dirs = path.split("/");
|
||||||
|
if (dirs.length > 1) {
|
||||||
|
String pathWithoutFileName = path.replace(dirs[dirs.length - 1], "");
|
||||||
|
if (!Files.exists(Paths.get(pathWithoutFileName))) { //create dirs if they don't exist
|
||||||
|
if (!new File(pathWithoutFileName).mkdirs()) {
|
||||||
|
throw new IOException("An error occurred while creating directories");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeFile(path, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a file to disk
|
||||||
|
*
|
||||||
|
* @param filename the file name (and its extension if wanted)
|
||||||
|
* @param content the content to write
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static void writeFile(final String filename, final String content) throws IOException {
|
||||||
|
final BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
||||||
|
writer.write(content);
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a JSON object to String
|
||||||
|
* toString() does not produce a valid JSON string
|
||||||
|
*/
|
||||||
|
public static String jsonObjToString(JsonObject object) {
|
||||||
|
return JsonWriter.string(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a JSON array to String
|
||||||
|
* toString() does not produce a valid JSON string
|
||||||
|
*/
|
||||||
|
public static String jsonArrayToString(JsonArray array) {
|
||||||
|
return JsonWriter.string(array);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user