Java: How to use AbsolutePath as a Link - java

I´m writing some Java and one of my task is to convert the Input/Output in to html. Well everything is going good, I can read some files and get the path of those files. Now I´m trying to convert the path in to a normal link so:
"<a href=" + file.getAbsolutePath() + " target=_parent>"
Works pretty good, but:
One, the explorer show an error that it can´t read the file, example:
a jpg or a word file..
Two, if the path have a blank like: "my picture.jpg"
recognize everything after the blank as a normal text...
can someone give me a Tipp, How to fix that, or I just using the wrong method?

You can use toURI() method that turns the file path into a URI:
file.getAbsoluteFile().toURI().toString()

Related

How do I get the folder name from a String containing the Absolute file path in android?

Path name is : /storage/emulated/0/Xender/video/MyVideo.mp4
I am able to get last file name [MyVideo.mp4] from path using
String path="/storage/emulated/0/Xender/video/MyVideo.mp4";
String filename=path.substring(path.lastIndexOf("/")+1);
https://stackoverflow.com/a/26570321/5035015
Now i want to extract path [/storage/emulated/0/Xender/video] from this path.
I have one use of this path in my code so that i want to do this like this.
How can i do this?
Any help will be appreciated.
new File(path).getParentFile().getName() should work.
With regards to your current code, don't implement your own path parser. Use File.
Also note that this has nothing to do with Android specifically; this is a general Java question.

How do I write/read/delete contents of a text file located in my resources folder?

I have a text file in my resource folder and I need to be able to read/remove/add text. I believe I have to use append so I tried this.
Files.write(Paths.get(Testing.class.getResource("/testresources/SearchList.txt").getPath())
, "the text".getBytes(), StandardOpenOption.APPEND);
This gives me back
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /C:/Users/Ben/workspace/Eve/bin/org/me/Testing/resources/SearchList.txt
If anyone could should me some clarity on this subject that would be great. Thankyou!
This worked for me fine:
You should add the text file to a raw folder. If the raw folder does not exist in your resource directory, create one. And then in order to access it and write or edit it, use this:
OutputStream outputStream=getResources().openRawResource(R.raw.YourTextFile);
then in order to Read from the file, you should do this
InputStream inputStream=getResources().openRawResource(R.raw.YourTextFile);
A leading slash is being automatically added to your path - /C:/Users/Ben/workspace/Eve/bin/org/me/Testing/resources/SearchList.txt
Lots of solutions have been suggested here in answers, Java NIO file path issue
Use any of those mentioned answers as your solution.
Also, for files intended to be in jar, You need to use Paths method public static Path get(URI uri) instead of public static Path get(String first,String... more) See How to get a path to a resource in a Java JAR file

Extracting files from res folder in Executable JAR (txt files to be specific)

I would like to ask if its possible to put text files into my jar, I use them to make my map in my game, but users can get Highscores. now I want to save the Highscores with the map, so I have to save the map on the user their PC. Is there any way how I could do this? I've searched the internet for some ideas but I could not find anything that even came close to what I've wanted. I only had 3/4th of a year java so I don't know much about these things, everything that happens outside the debug of eclipse are problems for me(files are mainly one of those things, null exceptions, etc).
The main question now.
Is it possible to do? If yes, do you have any terms I could search on, or some sites/guides/tutorials? If no, is there any other way how I could save the highscores?
EDIT:
to make clear
Can I get the text file (the text inside the file) to be extracted to a different file in like the home directory of my game (where I save the settings and stuff) the basic maps are inside the jar file, so I want them to be extracted on the first start-up of the program
Greetings Carolien
"extracted to a different file in like the home directory of my game (where i save the settings and stuff) the basic maps are inside the jar file, so i want them to be extracted on the first startup of the program"
You can get the URL by using getClass().getResource()
URL url = getClass().getResource("/res/myfile.txt");
Then create a File object from the URI of the URL
File file = new File(url.toURI());
Then just perform your normal file operations.
if (file.renameTo(new File(System.getProperty("user.home") + "\\" + file.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
Assuming your file structure is like below, it should work fine
ProjectRoot
src
res
myfile.txt
Note: the above is moving the entire file. If you want to extract just the data inside the file, then you can simple use
InputStream is = getClass().getResourceAsStream("/res/myfile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The just do normal IO operation with the reader. See here for help with writing the file.

Android (Eclipe) Video View File name path?

I'm here with what I think to be an easy doubt, but it's blowing my mind >.<
I'm pretty newbie to Java, and I wanted to make an app for android, but I'm stuck with the video player's coding.
I'm using this code, which I found on the internet (I think it was in stackoverflow, but not sure)
VideoView vv = (VideoView)this.findViewById(R.id.videoView1);
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.reveal;
vv.setVideoURI(Uri.parse(fileName));
vv.start();
what the heck I put on the filename, where it has "android.resource://"
I want to play a video when I open a certain activity. My video is located in the drawable-ldpi's folder.
The video's file name is reveal.wmv
Can you guys help me solving it?
Best regards :D
Sorry about my english :/
You are thinking about it a little wrong. I know that in most tutorials they use "filename" as somewhere that you should insert the name, but in this case it is not so. The line
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.reveal;
is setting the file path and filename of the video. In this case, it is setting a path to the raw folder and to the video "reveal" If you have not already, you should create the raw folder and place it in the res folder. That is where you will store all video and sound media.
In the next line:
vv.setVideoURI(Uri.parse(fileName));
the "filename" is substituting for the enter path, so leave that as it is. In the previous line you named the String that is pointing to the video, "filename" It could also have been programmed as so:
String elmo = "android.resource://" + getPackageName() + "/" + R.raw.reveal;
vv.setVideoURI(Uri.parse(elmo));
I have seen that it is best to use mp4 format files. I know that is just the extension and I am not sure about the actual codec, but that should lead you in a direction. Notice that the extension is not used in the file name itself. Android will simply search for the name and load the file regardless. If it is the wrong format or acceptable extension, it simply will not play.
Hope that helps.
The drawables folder is not for videos. Put it in res/raw/ (you may need to create). You will need to change the resource name (R.raw.yourfilename), but other than that, your example looks correct.
The way you're trying to declare the PATH is wrong ... first you have to put your video file in /asset folder and then use getAssests() method .. it 'll work.
Just move reveal.wmv from drawable-ldpi folder to raw folder, then it will work well. If you don't have raw folder, create it under res folder.

AudioInputStream not working

When I embed my resource and use the follwoing:
getClass().getResourceAsStream("sound.wav")
I get the following:
could not get audio input stream from input stream
If I link directly to the file it works fine.
If I link directly to the file it works fine.
It seems that you mean File or URL by that. (Can you confirm that & which one you mean, if so?) In that case, you'll often find that Java Sound requires a repositionable InputStream, which is (strangely) not what getResourceAsStream() returns.
The solution to that problem is to load the sound from URL. Obtain the URL using something like:
URL urlToClip = this.getClass().getResource("sound.wav");
// sanity check!
System.out.println("urlToClip: " + urlToClip);

Categories

Resources