Reading specific String from text file using non-activity Java Class - java

I have a text file and it has -
packagename:com.hello
I have non-activity Java Class which has to read this text file fetch this com.hello and output it in the form of Log or Toast Message. I am doing Programming in Android in Eclipse. I have 2 questions..
1) Where do I need to place this text file I mean the location of it so that my JAva Class can read it.
2) Since my JAva Class is non-activity class, openFileInput is not working since it needs context and I have no way of getting context.
FileInputStream in = openFileInput("filename.txt");
Is there any way of doing it. Thanks in advance :)

1) Where do I need to place this text file I mean the location of it so that my JAva Class can read it.
Anywhere you want, just tell your app the correct path to the file.
2) Since my JAva Class is non-activity class, openFileInput is not working since it needs context and I have no way of getting context.
Just read the file in Java!!!
FileInputStream fis = new FileInputStream(new File("path/to/your/file.txt"));
NOTES:
you must throw or catch a FileNotFoundException
remember closing the stream when finished!!

1)You can place it anywhere inside your package ; just ensure you provide the correct path. 2)Refer this : How can I read a text file in Android?

Related

android create text file on install

I need help storing data on my Android app. I need to create a text file, but only once. I have tried if(file.exists()) and if (file!=null) but nothing's working.I need this text file to store user data strings between app restarts. This code will create the file onCreate everytime, but I need it to only do it if the file doesnt already exist
private void createFile(String filename){
if(memoryFile != null){
memoryFile = new File(getApplicationContext().getFilesDir(), filename);
}
}
This code will create the file onCreate everytime
No, it will not. It will not create any file. It creates an instance of a File object. That is a Java object that represents a (possible) file on the filesystem. It does not actually create the file. To create the file, write something to it, using a FileOutputStream (and a background thread). To see if the file already exists, call exists() on memoryFile.
Also, note that you do not need getApplicationContext() here. Just use getFilesDir().

Where do i put file(.mp3) in order for it to be read?

I am usinf the JavaFx method of playing music files but it isn't working (sound not playing). I feel the problem lies in my files location. Where do I put the .mp3 in my java projects folder for it to be referenced with a simple string as so? Or is there another way to reference it? JavaFX Media takes a String parameter.
String test = "test.mp3";
Media x = new Media(test);
MediaPlayer mediaPlayer = new MediaPlayer(x);
mediaPlayer.play();
If you have read the JavaDoc of Media, then you know that you have to give a http, file or jar URI to the constructor you are using.
Actually from looking at the source code I wonder that you are not getting an IllegalArgumentException.
However, use Class.getResource(...).toURI().toString() to get the String you want to give to the Media constructor.
The combination of which Class object you call this on and what you give to the getResource() method depends on how you layout your files.
If you have your file besides your class, getClass().getResource("file.name") should work.
If you have your file in the root of your classpath, getClass().getResource("/file.name") should do.
You can give any other valid http, file or jar URI to the contructor too of course.

How to Get reference of Opened file in Java?

I want to build a method that opens an existing file and returns a reference to said file (Like a file handle) as an object. If I succeed in opening this file, then the fields of the file handle should be initialized with the information about the opened file, like for example the number of blocks stored in that file. Any Idea how to go about this? Any suggestions would be appreciated.
File fileReference = new File("/path/to/your/file");
if (fileReference.isFile()) {
long length = fileReference.length();
// etc.
}
See more available methods on the File class: https://docs.oracle.com/javase/7/docs/api/java/io/File.html

Android Studio Helper Object

In my new app I wanted to make an IO object that will help me access text files written in a raw folder located in the res folder.
The object is a java class that is written in the same location as all of the rest java files.
The goal of this object is that I can access files from all of the activities instead of writing them again and again.
But when I tried to do that I stumbled upon a few errors that I can't find a way to fix them.
While writing the code:
FileOutputStream fos = new FileOutputStream(R.raw.fName);
(fName is a Sting that the action receives)
Android studio marked the "raw" red and said it didn't recognize it.
From what I understand the R file doesn't recognize the raw folder.
The second problem is , when I write the code:
InputStream is = Context.this.getResources().openRawResource(R.raw.fName);
It still gives me an error at the raw and an error at "Context.this" , and asks me to split the deceleration or to surround it with a try block , which doesn't help. And at the headline there is a throws IOException so any errors with IO should be cleared.
And if there is an easier way to access actions in java files , a link or an explanation will help a lot.
P.S. a raw folder is a premade folder with premade text files , also an easier way to read and write in premade files will help.

Android get file using path (in String format)

My app needs to get an existing file for processing. Now I have the path of the file in String format, how can I get the File with it? Is it correct to do this:
File fileToSave = new File(dirOfTheFile);
Here dirOfTheFile is the path of the file. If I implement it in this way, will I get the existing file or the system will create another file for me?
That's what you want to do. If the file exists you'll get it. Otherwise you'll create it. You can check whether the file exists by calling fileToSave.exists() on it and act appropriately if it does not.
The new keyword is creating a File object in code, not necessarily a new file on the device.
I would caution you to not use hardcoded paths if you are for dirOfFile. For example, if you're accessing external storage, call Environment.getExternalStorageDirectory() instead of hardcoding /sdcard.
The File object is just a reference to a file (a wrapper around the path of the file); creating a new File object does not actually create or read the file; to do that, use FileInputStream to read, FileOutputStream to write, or the various File helper methods (like exists(), createNewFile(), etc.) for example to actually perform operations on the path in question. Note that, as others have pointed out, you should use one of the utilities provided by the system to locate directories on the internal or external storage, depending on where you want your files.
try this..
File fileToSave = new File(dirOfTheFile);
if(fileToSave.exists())
{
// the file exists. use it
} else {
// create file here
}
if parent folder is not there you may have to call fileToSave.getParentFile().mkdirs() to create parent folders

Categories