I have a problem and can't find a good solution to it. I need to read a textfile with large amount of data (file has 16MB). The file contains 12 columns with integer values in each one. Generally my problem is how to do this without freezing the app. I have my file in the assets folder of my project and I tried using something like this:
AssetManager assetManager = this.getAssets();
try {
InputStream inputStream = assetManager.open("3333.ecg");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
But app freezes. My goal is to get the data from each column and save it into an arraylist of integers. I'm looking for some advice.
Thanks in advance.
Use this to run it in background:
AsyncTask.execute(new Runnable() {
#Override
public void run() {
AssetManager assetManager = this.getAssets();
try {
InputStream inputStream = assetManager.open("3333.ecg");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
Related
so Im trying to develop a "module/addon" system and I want to read a "module.json" file from all modules/addons loaded in a direcory. So I got the part with looping through the files and stuff. But I dont know how I can access that "module.json" file.
Here is what I tried:
try {
for (File file : this.modulesDir.listFiles()) {
if(file.getName().endsWith(".jar")) {
InputStream stream = new URLClassLoader(new URL[]{file.toURL()}).getResourceAsStream("./module.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
while((builder.append(reader.readLine())) != null);
reader.close();
stream.close();
System.out.println(builder.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
Alright! I fixed it by changeing the path of the file to module.json instead of /module.json
Heres the code:
try {
for (File file : this.modulesDir.listFiles()) {
if(file.getName().endsWith(".jar")) {
InputStream stream = new URLClassLoader(new URL[]{file.toURL()}).getResourceAsStream("module.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
builder.append(line);
}
System.out.println(builder.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
Is it possible to read a file from internal storage in a utility class that doesn't extend anything? I have the code below to read a file, but I can't use the openFileInput method because it doesn't exist. Is there something I have to import to read these files?
private String readFromFile(String fileName) {
FileInputStream fis = null;
try {
fis = openFileInput("Links"); // this doesn't compile in a utility class
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
return String.valueOf(sb);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I'm implementing custom script rule plugin for Sonar.
I want to make a checking rule directly for the source code
and not from checking tokens or nodes of the ASTtree.
Having the follow code:
#Override
public void visitFile() {
BufferedReader br = null;
File file = null;
String line = null;
try {
file = this.getSourceCode().getFile();
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
...
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
My problem is that the :
this.getSourceCode().getFile();
returns back null
how can I get the instance of the file for which was actually the visitFile() called?
How does 'visitFile()' works actually?
So i have recently dove head first into java trying to learn lots of things. Recently i have been studying File Writers and Reader and Buffered Writers and Readers. I have recently came to a haul though every time i turn on the application the text file is modified agian. Is there a way that my text file can update every time i change a string. So basically it will read the file on boot and compare it to the string.
Here is my example of reading a text file and turning it into a string
private void Read() {
try(BufferedReader br = new BufferedReader(new FileReader(version))) {
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch(IOException e) {
e.printStackTrace();
}
}
All i need to do is it to compare this string with another. Thank you for your time.
** Update**
So i compared the two threads and now nothing is being written to the text file
private void Update() {
try {
fw= new FileWriter(version.getAbsoluteFile());
bw = new BufferedWriter(fw);
try(BufferedReader br = new BufferedReader(new FileReader(version))) {
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
if(!sCurrentLine.equals(VanoEngine.TITLE)) {
bw.write(VanoEngine.TITLE);
}
}
} catch(IOException e) {
e.printStackTrace();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
** Update**
Closed the stream #JavaNoob and still nothing is being written
private void Update() {
try {
fw= new FileWriter(version.getAbsoluteFile());
bw = new BufferedWriter(fw);
try(BufferedReader br = new BufferedReader(new FileReader(version))) {
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
if(!sCurrentLine.equals(VanoEngine.TITLE)) {
bw.write(VanoEngine.TITLE);
}
}
bw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
if(!sCurrentLine.equals(someInputString))
//write `someInputString` to file, because it differs with the one read from file
you can use sCurrentLine.equals("your string").
I am using BufferedReader to read file in java.
Following is the code snippet:
String line;
br = new BufferedReader(new FileReader("file1.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//Here I want to open file named "file2.txt".
As mentioned in the code snipped above, i want to now open a new file.
What is the best way to do so ?
Should i first close br using br.close, and then again initialise br or what ?
P.S.: I am new to Java.
Creating a method will make your code modular and easy to use. This will lead to re-usability of code and ease of understanding. Here is the sample code:
public static void main(String args[]) {
readFile("C:\\sample.txt");
}
public static void readFile(String filename) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(filename));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I would make a method and call it twice
void readFile(String fileName) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
...
}
}
note that BufferedReader instance br will be closed automatically and make sure that you are using JDK 7 for this
File I/O operations internally use Decorator pattern. So, .close() on the outermost object should close all internal I/Os
Yes, close it first, but use this pattern ...
BufferedReader br = null;
String line;
try {
br = new BufferedReader(new FileReader("file1.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
finally {
if (br != null) br.close();
}
//Here I want to open file named "file2.txt".
Or, the try-with-resources approach (semantically equivalent to the above) ...
String line;
try (BufferedReader br = new BufferedReader(new FileReader("file1.txt")) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
//Here I want to open file named "file2.txt".