I am storing my file in /data directory.
inputStream = new FileInputStream(Environment.getDataDirectory()+"/logfile.txt");
BufferedReader r = new BufferedReader(new InputStreamReader(
inputStream));
while ((line = r.readLine()) != null)
{...}
After I change my file and upload it to the device using adb and run the app, the app still continues to read the unchanged file from somewhere. But when I start to debug to see whats happening, its reading the modified file.
I am not sure if its caching it somewhere.
Please help.
Related
I'm using Netbeans and in my program I'm reading a file. When I run the program it reads the file correctly. When I build the program the JAR also works correctly. But when I change the file that I am reading from, in my build dir, my JAR doesn't update accordingly. Why is that so? Is there a solution for this?
The code below shows how am I reading the file in my program. Thanks in advance.
InputStream in = NewJFrame.class.getResourceAsStream("/holidays.txt");
BufferedReader readHolidays = new BufferedReader(new InputStreamReader(in));
String line;
line = readHolidays.readLine();
while(line != null) {
//read into hashmaps
//...
line = readHolidays.readLine();
}
readHolidays.close();
Never mind, i got it ;) If anyone else needs this: Basically every time i run the program i find the file path to the jar file and from there i read text document.
File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
FileInputStream is = new FileInputStream(jarFile.getParent().toString() + "/file.txt"); BufferedReader readFile= new BufferedReader(new InputStreamReader(is));
I'm new to coding and have decided to start my learning on Java. I've got NetBeans and have started to create a very basic web application. I'd like to be able to display values from a .txt file onto the webpage, and I've got this code to do so.
<%
BufferedReader in = new BufferedReader(new FileReader("Cats.txt"));
String line;
while((line = in.readLine()) != null)
{
out.println(line);
}
in.close();
%>
My text file is in the same folder as my src folder (As I've seen you need to put the file)
However, whenever I navigate to the web page I get a FileNotFound error. I've tried placing the files path in the FileReader but that gives an error due to the backslashes.
If anyone could help I'd be greatly appreciated
Currently it's looking for the file in the src directory of your application you should just be able move the file there and it should read it. If you would like to direct to a specific path you need to tell the IDE to treat the '\' as a normal slash to do this you need to close it off by using two '\'s instead of one eg:
<%
BufferedReader in = new BufferedReader(new
FileReader("C:\\MYPATH\\MYPATH2\\Cats.txt"));
String line;
while((line = in.readLine()) != null)
{
out.println(line);
}
in.close()
%>
I have a project in our university and in order to make it we have to read a txt file(in java,eclispe). I take the file and drag and drop (the method which reads the file they gave it ready to us) but when I run the program it gives me this:
I do not know if the mistake is how I put the file in the Eclipse
The folder name above src is non-English so I can't type it out, but suppose it were just Laur155. Suppose that this folder is directly under ~/workspace. Then to open the file you would do
String filename = "~/workspace/Laur155/NBA-5d-17265n.txt";
then something like
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) { ...
No we must not change the code in the method which reads the txt file
I know maybe i confuse you but in my university they havent teach as nothing about files
I have an Android project that displays data from a JSON file. The file is read from the assets directory, following the approach below:
src/main/assets/my_file.json
InputStream is = getResources().getAssets().open(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// use StringBuilder to get file contents as String
I also have local unit tests which require the same JSON file. In order to avoid the slowness of Instrumentation tests, I am currently loading a duplicate of the file as below:
src/main/resources/my_copy.json
File testData = new File(getClass().getResource(filename).getPath());
InputStream is = new FileInputStream(testData);
// read File as before
Is there an approach that would allow the JSON file to be stored in one location, whilst running the unit tests on a local JVM?
If you're using Robolectric, see this answer.
Note that the "assets" directory is an Android concept, it's different from Java resources. That said, you could also move your JSON file from assets to resources, and use it from both Android and JVM form there like you would in any Java application.
Files in the resource directory can be accessed within Android applications by using ClassLoader#getResourceAsStream(). This returns an InputStream, which can then be used to read the file. This avoids having to duplicate files between resources and the assets directory.
InputStream is = getClass().getClassLoader().getResourceAsStream("my_file.json");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
String json = sb.toString();
I am trying to open an existing file, and process it, and save it somewhere else in Android.
File in = new File("/sdcard/a.pdf"); // This file exists in the location and has been obtained by using getExternalStorage()
FileInputStream fis = new FileInputStream(in);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
header = br.readLine(); // this gives first line in android 2.3.3 but gives null in android 2.3.6
Executing the above code in Android 2.3.3 gives me the header while executing it in Android 2.3.6 gives "null" in the header.
What may be the problem?
Please Help.
Make sure you explicitly specify the charset to use in each case. At the moment you are relying on the default, which may vary between platforms.
Try the alternative InputStreamReader constructor instead:
new InputStreamReader(fis, "UTF-8")