Java reading file txt from URL - java

I would read txt content from web, I know how to read a local file but I want to put my txt file in a website and read it in my application. I used this code :
BufferedReader reader = new BufferedReader(new InputStreamReader((new URL("MY TXT URL")).openStream()));
String line = reader.readLine();
int k =0;
String[] SaveLine = new String[20];
for( k = 0; line!=null; k++)
{
System.out.println(reader.readLine());
line=reader.readLine();
}
There is a problem, with this code I read the entire webpage, so the HTML is included. How can I just read the file content ? There is a specified website where I could put my txt files for read them?
Thanks in advance!

If this "MY TXT URL" is indeed your TXT file's URL
(for example http://www.test.com/file10.txt), then your
code will read the TXT file itself, and not any web page (as you say it does).
So you're mixing something here.
Whatever URL you give to it, that web resource (file) it is going to read.

This is a very confusing problem. Try this. Make the URL include the location and name of the textfile; here is an example: www.yourwebsiteurl.com/yourtextfile.txt. Using that method, ONLY the textfile, not including HTML, will be read.

If the endpoint of your URL is a text file, if shouldn't be displaying any html (unless that's what's contained in your text file.)
allegedly uploadedit works though I haven't used it myself.

Thanks to all, I tested my code in a txt uploaded on DropBox and Dropbox probably has code system that I don't know, infact my code returns html/script and not the content of txt. So probably the correct ask is if anybody know a website where can I put my txt file just to reading it? (the url must don't change for ever)
EDIT: PROBLEM SOLVED
I put my file in a website to just read the file and don't read it in a html box (such as a lot of cloud website)

Related

Read and Append data to the File from a Blob URL path before download

This is my first hands on using Java Spring boot in a project, as I have mostly used C# and I have a requirement of reading a file from a blob URL path and appending some string data(like a key) to the same file in the stream before my API downloads the file.
Here are the ways that I have tried to do it:
FileOutputStream/InputStream: This throws a FileNotfoundException as it is not able to resolve the blob path.
URLConnection: This got me somewhere and I was able to download the file successfully but when I tried to write/append some value to the file before I download, I failed.
the code I have been doing.
//EXTERNAL_FILE_PATH is the azure storage path ending with for e.g. *.txt
URL urlPath = new URL(EXTERNAL_FILE_PATH);
URLConnection connection = urlPath.openConnection();
connection.setDoOutput(true); //I am doing this as I need to append some data and the docs mention to set this flag to true.
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("I have added this");
out.close();
//this is where the issues exists as the error throws saying it cannot read data as the output is set to true and it can only write and no read operation is allowed. So, I get a 405, Method not allowed...
inputStream = connection.getInputStream();
I am not sure if the framework allows me to modify some file in the URL path and read it simultaneously and download the same.
Please help me in understanding if they is a better way possible here.
From logical point of view you are not appending data to the file from URL. You need to create new file, write some data and after that append content from file from URL. Algorithm could look like below:
Create new File on the disk, maybe in TMP folder.
Write some data to the file.
Download file from the URL and append it to file on the disk.
Some good articles from which you can start:
Download a File From an URL in Java
How to download and save a file from Internet using Java?
How to append text to an existing file in Java
How to write data with FileOutputStream without losing old data?

How to rename file forth and back in java

I want to rename a file name xxx.docx to xxx.docx.zip then rename it back to xxx.docx in Java.
Here is my code.
File file = new File(path);
File file2 = new File(path+".zip");
file.renameTo(file2);
File file3 = new File(file.getPath());
file2.renameTo(file3);
It won't work. Thank you.
Edit : The problem is I forgot to close the doc before renaming it.
The code like that works. Most probably some other process has locked the file and made it read only. You have either opened it in word (since it is docx file) or something like that. Maybe it is in a readonly location.
The code is working though. Try with different file and you will see it is fine (I tried it).

Input Stream from ZipResourceFile talking lot of time to read data

I have successfully implemented Apk Expansion Files for my project.
Problem:In my .obb i have a folder which has 100 xml files in it.Now the problem is i am using the below code to read the data directly from .obb files without extracting the data.
this is code given in the offical doc here http://developer.android.com/google/play/expansion-files.html under the topic Reading from a ZIP file
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(MyActivity.this, 1, 0);
String pathToFileInsideZip = "main.1.com.my.expansionfiles.obb/data/" +filename;
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
i have a for loop in that i am calling writing this code so that it will read all the xml one by one and make the data ready for me to display.
The above will read the data directly from .obb file, but the problem is its talking lot of time to extract the data?
Why so? i am doing any mistake here?
I believed that the pathToFileInsideZip would not be
main.1.com.my.expansionfiles.obb/data/[files].
I think it's just
"data/[files]"

How do I "import" a HTML file from a shared drive into a JSP from my war file?

Preface: I am an inexperienced java programmer handed one of his first assignments. If I do not ask the question correctly or do not give enough detail, please let me know.
I am trying to import a HTML page that is saved on my C drive. I am trying to import it to the content portion (div id="content") of a JSP file that exists in a war file. I have already figured out that I can not use jsp:include, #include, #include file because the file exists outside the war file. I also figured out that c:import and iFrame do not work.
My goal is to make the contents of the html file that is saved in on my c drive appear in the contents of the jsp (visible on the web page).
Am I on the right track with this <% File f = new File("c:\\temp\\filename.html").......%>
I have searched stackoverflow and the only topic that came close was "How to Include a file outside the application (war) using jsp include." It did not really get me where I needed to go. Maybe the answer is right in front of me but I couldnt see it.
JSP/JSTL does not offer tags which support this. You'd need to do it using pure Java. You just have to write it to the response yourself.
Here's one of the simplest ways:
<%
Reader reader = new FileReader("c:/path/to/external/file.html");
try {
for (int i = 0; (i = reader.read()) != -1;) {
out.write(i);
}
} finally {
try { reader.close(); } catch (IOException ignore) {}
}
%>
You could wrap it in a custom tag to keep your JSP free of scriptlet clutter, or you could read it into a String in a servlet and pass it to JSP EL scope.
Do you want clients to see the contents of filename.html located on your server? If so, why don't you just get it inside your project/war?
Or do you want clients to see the contents of a filename.html they have on their computers? If so, you might be able to just add an iframe with that source... but you'll run into many security-related problems, since browsers won't ordinarily let you do that.
Try
<% File f = new File("c:\\temp\\filename.html");
BufferedReader in = new BufferedReader(new FileReader(f));
while (in.readLine() != null) {
out.println(blah blah blah);
}
in.close();
%>
reading the File and Printing it to the JSP should work,
My suggestion is to use http form upload in your jsp application. In that case your file can be in any place that is accesible in your filesystem instead of hardcoding it to be in a certain place. Usage http://commons.apache.org/fileupload/using.html
Good tutorial on http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html
Some useful hints can also be found in the video, http://www.youtube.com/watch?v=BLamJlRg9Ws

Java read/write text file on a website

Basically I've uploaded a text file to my host and I want to edit the file and read it with java. I've created the permissions for it but im not sure how to do it with Java. This is my code which read/writes locally:
Read:
BufferedReader mainChat = new BufferedReader(new FileReader("./messages/messages.txt"));
String str;
while ((str = mainChat.readLine()) != null)
{
System.out.println(decrypt.Decrypt(str, salt));
}
mainChat.close();
Write:
FileWriter chatBuffer = new FileWriter("./messages/messages.txt",true);
BufferedWriter mainChat = new BufferedWriter(chatBuffer);
mainChat.write(message);
mainChat.newLine();
mainChat.flush();
mainChat.close();
How would I have to modify this to make it work? Thanks
I don't think you can read/write directly to a file on a web server the way you would on a local filesystem. What you'll probably need to do is:
download the file
open it in the local editor
when it's saved, automatically re-upload the file
You can do this all within the editor, and hide this in the app by having it do the download-edit-save-upload in the background. Many text editors will do this by establishing a remote connection similarly, and making the file writing round trip transparent to user.
You should implement some sort of remote procedure call. Basically, from the client send the server a message containing what you'd like to put in the file. Then have the server actually open the file and write the content of the message to the file.

Categories