Java read/write text file on a website - java

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.

Related

FileWriter to write to user desktop in Java, rather than server desktop

As part of a search application, I want the user to be able to download a report showing the results in a CSV file. I have the following method:
public void downloadCustomerResults(String customer) {
String output = "";
output += produceCustomerID(customer);
output += produceCustomerAddress(customer);
output += produceCustomerContactDetails(customer);
output += produceOrderHeader(customer);
output += producePayments(customer);
// Writes to server desktop, not user desktop.
try {
Writer fileWriter = new FileWriter("C:\\Users\\username\\Desktop\\SAR" + customer + "C.csv");
fileWriter.write(output);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
This downloads the file to the desktop of the machine running the server, not the user's desktop (accessing the app via JSP's on Tomcat). How would I change the file path string to make this download to the users' desktop? Or would I have to pass the file to the JSP for the user to download via their browser?
Thanks.
Short answer: The server has no means of accessing the client's filesystem.
Longer answer: You might either provide a service for the client to download the file (e.g. a webservice accessible through a URI, like #Kayaman mentioned) or the client provides you a service to write the file (e.g. a remote file system, an FTP server etc.). For the latter there might be libraries providing a special java.nio.FileSystem extension.
You may also provide an application running on the client to receive the file. This client application will then have acces to the client's file systems (unless it lacks the access rights, of course).
So the answer I found was to use the JavaScript package FileSaver.js.
This accepts a blob created from a string, and then saves it with a filename of your choice to the browsers preferred download folder.
I managed to pass the string from Java to JavaScript, and then pass it through FileSaver.js in the .JSP page.

Read a file from shared location with access restrictions (java)

I have a program that has to read a file from network location - something like this
String sFileSource = "//MyShared/location/fileName.txt" ;
File inputFile = new File(sFileSource);
try {
ffBuffer = new BufferedReader(new FileReader(inputFile));
}
catch (FileNotFoundException e) { // should never happen
}
Now, the problem is that that shared location is on the different network domain and accessible only using domain credentials
How can I embed entering the credentials into this java program ? The problem is that when ran from different PCs it fails due to login.
Reading a file like that is not a secure way to do it, because you will expose your user domain credentiels.
Reversing the java app could lead to that, so it's better to use an ftp server for that.
The way I have done it before:
Read remote file in java which needs username and password

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?

Android - Replace a text file from a server with an extended line

I have a text file on a server, now if the user presses a button, the text file should get a new line .
My idea was to create a text file on the device...to read the actual text file of the server...to append 'this new line' and upload it to the server, but this way isn't safe, are there other ways to do this ?
Thanks in advance !!
if the server is a Web Server, you can simply send an Http request (Using HttpUrlConnection for example) from your application to a specific PHP file on the server, and put in this file (PHP file) the code to append a new line to the file or event to append a custom text content which you send it with the request.
No need to read the existing text from the server unless you need them on the device, if you you need is to add a new line of text, you just need to send the text from the device to the server and and append the text to the text file on the server.

Java reading file txt from URL

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)

Categories