I get a IO Exception java.io.FileNotFoundException:(Filename too long) when I use a BufferedReader to read a file from a url(response is the url response).
String payload = response.readAsString();
try(FileReader reader = new FileReader(payload);
BufferedReader bufferedReader = new BufferedReader(reader)) {
The problem seems to be that the contents of the file is read as the file name and it's longer than what's allowed.
To get around this I've used PrintWriter to write the contents to a file and am reading that file but would like to know if there's a better way to do this.
I went down a different route in the end.
String payload = response.readAsString();
try{
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(payload);
JsonNode vaultsNode = root.path("responseData").path("vaults");
...
Related
I need to read a sample.csv file from the same class as my main.
Below you can see the structure.
This is my code but when I run it it gave me the error that the sample.csv is not find!
public int convertFile(String csvFileName, String xmlFileName,
String delimiter) {
int rowsCount = -1;
BufferedReader csvReader;
try {
Document newDoc = domBuilder.newDocument();
// Root element
Element rootElement = newDoc.createElement("XMLCreators");
newDoc.appendChild(rootElement);
// Read csv file
csvReader = new BufferedReader(new FileReader(csvFileName));
CSVReader reader = new CSVReader(new FileReader("sample.csv"));
//CSVReader reader = new CSVReader(csvReader);
String[] nextLine;
int line = 0;
List<String> headers = new ArrayList<String>(5);
while ((nextLine = reader.readNext()) != null) {
....
Can anyone suggest me what is the problem?
Without relative or absolute path, it is directly related to the "current" directory. So it does NOT matter so much that the .csv file is in a certain package, but it does matter that when the application is started the "working" directory is exactly the directory that contains the file.
You are using Eclipse, go to see the Run Configuration (Run menu -> Run Configurations ...) used to launch the application, Arguments tab.
you can get path like this.
URL url = YourClassName.class.getResource("sample.csv");
FileReader fileReader = new FileReader(url.getFile());
I have a String formatted as a JSON (with pretty print) that I would like to create a new JSON file.
How can this be done with the Google GSON library?
It seems that JsonWriter needs to be used, but I am having trouble finding how to actually write to a new file.
JsonWriter's constructor takes another writer as a parameter. You simply need to open a stream on the file you want to write to.
Path fileLocation = Paths.get("/.json");
try (Writer fileWriter = Files.newBufferedWriter(fileLocation, Charset.defaultCharset(), StandardOpenOptions.READ)) {
try (JsonWriter jsonWriter = new JsonWriter(fileWriter)) {
// Write your json object using jsonWriter.
gson.toJson(obj, jsonWriter);
}
}
I am building am app the will work offline.
I download the json to a file in the sdcard. then when I try and load it using gson and map it with POJO class I get error:
read the file:
File newList = new File(rootFolder.getParent(), "custom_list.new.dat");
String customListStr = Utils.readTextFile(newList);
public static String readTextFile(File file) throws IOException {
FileInputStream reader = new FileInputStream(file);
StringBuffer data = new StringBuffer("");
byte[] buffer = new byte[1024];
while (reader.read(buffer) != -1)
data.append(new String(buffer));
reader.close();
return data.toString();
}
try to load it
//Load json file to Model
Gson gson = new GsonBuilder().create();
final MoviePojo response = gson.fromJson(customListStr, MoviePojo.class);
int size = response.getContentList().size();
: Error: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3205 path $
The error shows you, in the text, that the JSON you have downloaded has an error in its structure MalformedJsonException, "Malformed". Check the JSON file, from top to bottom, may be some of this ("{", "[", ",", ":" ) characters are missing or has more than the quantity it needs.
Good luck.
I am trying to edit a file in internal storage (So no permission should be needed, right?), but when I run this code:
String locus = getFilesDir().getAbsolutePath();
File locusfile = new File(locus);
String loglocation = locus + "/log.txt";
File log = new File(loglocation);
if(log.exists())
{
PrintWriter pw = new PrintWriter(log);
}
the PrintWriter pw comes up with "FileNotFoundException".
Why would this occur even when I verify that the file exists?
Thanks.
Try to add log.createNewFile(); after File log = new File(loglocation);.
I have the following code:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.groovy");
I need to pass this resource to the method called parse which takes File as param, so doing so fails:
new GroovyShell().parse(new FileInputStream(in));
How I can convert FileInputStream to File in java?
You can actually use: GroovyShell#parse(Reader) method:
Script result = new GroovyShell().parse(new InputStreamReader(in));
use this :
try {
URL url = this.getClass().getClassLoader().getResource("test.groovy");
File file = new File(url.toURI());
new Shell().parse(file);
} catch(...){...}
if you know exact path to "test.groovy", do this:
new Shell().parse(new File("path/to/test.groovy"));