JavaScript:
Odczytywanie pliku tekstowego
How to: (Jak to zrobić:)
Reading text files in JS depends on the environment: browser or Node.js. Here are both:
Browser:
// HTML5 File API for reading files in-browser
document.querySelector('input[type=file]').addEventListener('change', event => {
const reader = new FileReader();
reader.onload = function(e) {
console.log('File content:', e.target.result);
};
reader.readAsText(event.target.files[0]);
});
Node.js:
const fs = require('fs');
// Async read from file system
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log('File content:', data);
});
Sample Output:
File content: Here is the text inside your file.
Deep Dive (Dogłębna analiza):
Historically, JS was client-side only, and file access was severely restricted for security reasons. Node.js introduced server-side JS, allowing file system access.
Alternatives:
fetch
in browsers for remote files.- Streams in Node.js for big files to prevent memory overload.
Implementation:
- The FileReader API in browsers works asynchronously.
- Node.js
fs.readFile
reads the entire file into memory, but you can usefs.createReadStream
for a more efficient approach.
See Also (Zobacz również):
- MDN Web Docs on FileReader API: MDN FileReader
- Node.js File System (fs) module: Node.js fs
- Node.js Stream API: Node.js Stream