在读取文件时,如果想避免跳过UTF-8 BOM,可以使用以下方法:
```python
with open('file.txt', 'r', encoding='utf-8-sig') as f:
content = f.read()
```
```java
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt"), StandardCharsets.UTF_8));
String line;
StringBuilder content = new StringBuilder();
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
reader.close();
```
```javascript
const fs = require('fs');
const iconv = require('iconv-lite');
const content = iconv.decode(fs.readFileSync('file.txt'), 'utf8');
```
通过以上方法,可以在读取文件时避免跳过UTF-8 BOM。
领取专属 10元无门槛券
手把手带您无忧上云