Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileReader to String

Consider the following code

function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var myFile = evt.target.files[0];
    var reader = new FileReader();
    reader.readAsText(myFile);
    var myString = reader.toString();
    alert(myString); // print  - "[object FileReader]"   
}

I try to get all the file content into String , for example if the file content is

helloWorld1
helloWorld2

I will get alert of that's content .

like image 890
URL87 Avatar asked Feb 22 '14 23:02

URL87


People also ask

How do I read a file into a string?

The readString() method of File Class in Java is used to read contents to the specified file. Return Value: This method returns the content of the file in String format. Note: File. readString() method was introduced in Java 11 and this method is used to read a file's content into String.

How do you read a text file as a string in Java?

Java read file to String using BufferedReader BufferedReader reader = new BufferedReader(new FileReader(fileName)); StringBuilder stringBuilder = new StringBuilder(); String line = null; String ls = System. getProperty("line. separator"); while ((line = reader. readLine()) !=

How do I create a Java string from the contents of a file?

String content = new String(Files. readAllBytes(Paths. get(filename)), "UTF-8");


1 Answers

That's not how you get the result of a FileReader. Modify your code to this:

function readSingleFile(evt) {
        //Retrieve the first (and only!) File from the FileList object
        var myFile = evt.target.files[0];
        var reader = new FileReader();
        reader.readAsText(myFile);
        reader.onload=function(){alert(reader.result)}
    }
like image 64
markasoftware Avatar answered Oct 13 '22 01:10

markasoftware