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
A Docker container starts a single process when i...
Method 1: Use table attributes: header-cell-class...
CentOS 8 is officially released! CentOS fully com...
Table of contents I. Definition 2. Usage scenario...
This article uses examples to illustrate the func...
First post the effect picture: A scroll bar appear...
Table of contents What are spread and rest operat...
1. Add fields: alter table table name ADD field n...
When learning about inline-block, I found that the...
Detailed example of removing duplicate data in My...
Requirements: The PC side and the mobile side are...
In order to solve the problem mentioned last time...
<br />It has been no more than two years sin...
This article shares the specific code of js to im...
1. Introduction Since pictures take up a lot of s...