JS obtains the .txt file content through FileReaderRecently, I have been dealing with a requirement to parse .txt files through js to do some processing. Here is a summary. Read .txt file methodvar reader = new FileReader(); var fileUploader = document.getElementById("fileUploader"); //Get the input box id to get the file information reader.readAsText(fileUploader.files[0], "utf-8"); //Set the encoding reader.onload = function() { undefined data.trim().split('\n').forEach(function(v, i){undefined window['str' + (i+1)] = v } }
There is no direct method to get the total number of lines in a .txt file, so I use a loop to handle it here: var count =0; data.trim().split('\n').forEach(function(v, i){undefined count++; }) JS: FileReader() reads filesThe FileReader object allows a Web application to asynchronously read the contents of a file (or raw data buffer) stored on the user's computer, using a File or Blob object to specify the file or data to read. property:
Let's start with a practical exampleindex.html is as follows <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>FileReader</title> </head> <body> <input id="input" type="file"> </body> </html> demo.txt is as follows
Reading txt files<script> const input = document.querySelector('input[type=file]') input.addEventListener('change', ()=>{ const reader = new FileReader() reader.readAsText(input.files[0],'utf8') // input.files[0] is the first file reader.onload = ()=>{ document.body.innerHTML += reader.result // reader.result is the result} }, false) </script> Reading image files<script> const input = document.querySelector('input[type=file]') input.addEventListener('change', ()=>{ console.log( input.files ) const reader = new FileReader() reader.readAsDataURL(input.files[0]) // input.files[0] is the first file reader.onload = ()=>{ const img = new Image() img.src = reader.result document.body.appendChild(img) // reader.result is the result of the acquisition} }, false) </script> Examples import java.io.*; public class FileRead { public static void main(String args[]) throws IOException { File file = new File("Hello1.txt"); // Create a file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); // Write content to the file writer.write("This\n is\n an\n example\n"); writer.flush(); writer.close(); // Create a FileReader object FileReader fr = new FileReader(file); char[] a = new char[50]; fr.read(a); // Read the contents of the array for (char c : a) System.out.print(c); // Print characters one by one fr.close(); } } method
event
The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: A brief discussion on the magical uses of CSS pseudo-elements and pseudo-classes
>>: How to use dl(dt,dd), ul(li), ol(li) in HTML
This article shares the specific code of js to re...
1. Some tips on classes declared with class in re...
1. First, an error message is reported when assoc...
This blog is a work note environment: nginx versi...
This article is welcome to be shared and aggregat...
The backend uses the thinkphp3.2.3 framework. If ...
Let's start with the body: When viewing a web ...
MySQL's index types include normal index, uni...
1. HTML tags with attributes XML/HTML CodeCopy co...
We often see a cool effect where the mouse hovers...
Servermanager startup connection database error R...
The pop-up has nothing to do with whether your cur...
Table of contents 1. After downloading, unzip it ...
Table of contents Simple Factory Factory Method S...
Recently, Microsoft released the 2019 server syst...