2

I need to open and edit a file present in my system using an online java compiler. How can I do this?

  • elaborate more about **online java compiler** – Exteam Jun 14 '19 at 06:30
  • Is it an option to make a web-server on your machine, so that the program running on the online java compiler host can access it? – Yunnosch Jun 14 '19 at 06:31
  • I should not download and use jdk on my system. Instead I want to run my program on any online website which provide java compiler. – Ajith prasad Jun 14 '19 at 06:32
  • 3
    It is not possible because browsers do not have the permission to access the content in your local disk. – Prasanth Ganesan Jun 14 '19 at 06:33
  • @Yunnosch yes, it is – Ajith prasad Jun 14 '19 at 06:34
  • @PrasanthGanesan is there any alternative solution to tackle this issue? – Ajith prasad Jun 14 '19 at 06:34
  • 1
    Why are you not installing a java compiler? – Prasanth Ganesan Jun 14 '19 at 06:35
  • The only way is to host your files in an FTP server and access it from online compilers. But you cannot have complete control over online compilers. You need to import some classes to read data from ftp server and I am not sure whether the online compilers will allow you to import all those packages. – Prasanth Ganesan Jun 14 '19 at 06:41
  • So what problem remains? You might need to rephrase your question to clarify. Are you asking how to access a webserver? Are you asking how to setup a webserver? Please be more specific. (Just to clarify, I think that otherwise a program running on a different machine than your own, i.e. the compiler host, cannot access files on your machine.) – Yunnosch Jun 14 '19 at 06:41

1 Answers1

1

It is not possible to read file content from your system because the browser do not have the permission to do so. The only way is to make use of an FTP service and read the content from it.

I have come up with a simple solution to read files from online a online file hosting site and use the read data in your code.

First I uploaded a test file in this website called uguu. After uploading my file the site provided me an API to download my file, But my file will be available for download only for the next 24 hours. That's the only catch.

After obtainin the API, I went to online GDB to check if I am able to read the test file. And I was able to read it without any problem.

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static void main(String args[]) throws MalformedURLException, IOException{
        BufferedInputStream in = new BufferedInputStream(new URL("https://a.uguu.se/wiaXcKwue2DH_cass").openStream());
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        System.out.println(result.toString("UTF-8"));
    }
}

Here is a screenshot of the output from online compileroutput screenshot

Edit 1: Thanks to Ferrybig for making improvements. My answer is based on this stackoverflow answer over here

Prasanth Ganesan
  • 541
  • 4
  • 14
  • This answer has the potential to correct text stored inside the file, this is because a string in java is not safe for any bytes, this becomes a problem when you read part of a character in 1 byte array, and the other half of the character in the other part of the byte array – Ferrybig Jun 14 '19 at 07:16
  • @Ferrybig to show what my program has read, the only way is to convert the bytes into String and print it in the console. If there is any other way to find what my program has read, please let me know. – Prasanth Ganesan Jun 14 '19 at 07:19
  • https://stackoverflow.com/a/35446009/1542723 Solution number 8 of that list has the highest performance, and doesn't suffer from the same text corruption problem, as the bytes are only converted once they are all collected. You are assuming 1 byte is 1 character in your answer – Ferrybig Jun 14 '19 at 07:24
  • @Ferrybig thank you for letting me know. I have made an improvement to my previous answer and changed it according to what you said. – Prasanth Ganesan Jun 14 '19 at 07:33
  • You should attribute it to me, but rather to the person who wrote the other answer – Ferrybig Jun 14 '19 at 07:34
  • Yes, just did it – Prasanth Ganesan Jun 14 '19 at 07:41