scala文件读取报错“java.nio.charset.MalformedInputException: Input length = 1”
今天写spark程序的时候遇到了一个问题就是,读取文件的时候报了一个错:“Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1”
读取文件的代码如下:

一看这个这个错“nio”错误,第一感觉就是读文件方法这里出了问题,于是点击去看了一下Source.fromFile这个方法的源码:

果然,这个方法的重载有好几个,但是每一都直接或间接的需要指定读取文件的编码,就是说你需要以什么编码去读取你需要读取的文件,因为我的IDEA设置的编码时UTF-8

所以如果使用UTF-8读取GBK文件会出现问题,如果查看一个文本文件是什么编码的文件呢:
使用notepadd++打开该文件再右下角会看到对应的编码格式:

说明该文件时UTF-8编码的。
so
需要指定读取编码为GBK

或者

这两中方式都可以,都是从源码中得来的。
设置完成之后,重新运行程序,报错消失
scala文件读取报错“java.nio.charset.MalformedInputException: Input length = 1”的更多相关文章
- protobuf接口调用报错:java.nio.charset.MalformedInputException: Input length = 1
使用protobuf定义的接口api发起http请求报错,日志如下: [-- ::] DEBUG AbstractPool: - server updated, node=, server={ nod ...
- windows中文编码报错 com.google.gson.JsonIOException: java.nio.charset.MalformedInputException: Input length = 1
昨天碰到一个问题:同一个请求页面,页面经过匹配后调用http的post协议接口,部署在linux环境的没问题,本地Eclipse启动的tomcat也没问题,直接启动本地tomcat却报错了: 18:4 ...
- org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
项目启动报错2018-12-21 14:06:24.917 INFO 23472 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refr ...
- 我的Java开发学习之旅------>Java NIO 报java.nio.charset.MalformedInputException: Input length = 1异常
今天在使用Java NIO的Channel和Buffer进行文件操作时候,报了java.nio.charset.MalformedInputException: Input length = 1异常, ...
- Caused by: java.nio.charset.MalformedInputException: Input length = 1
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/applicatio ...
- [bug] org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 2
原因 SpringBoot启动加载yml配置文件出现编码格式错误 参考 https://www.pianshen.com/article/2431144034/
- Flex文件读取报错
Flex文件读取 1.s:WindowedApplication <?xml version="1.0" encoding="utf-8"?> &l ...
- 转载:回编译APK出错:java.nio.char set.MalformedInputException: Input length = 1
使用APKtool回编译APK,出现错误如下: Exception in thread "main" org.yaml.snakeyaml.error.YAMLExcepti ...
- springcloud采坑--Zuul上传文件报java.nio.charset.IllegalCharsetNameException: UTF-8;boundary=sqgzzmMxl1UPdIp0IAYnQgUIAr9yNewVAzKIX
报错日志: 2018-12-17 10:01:19,688 ERROR [io.undertow.request] (default task-3) UT005023: Exception handl ...
随机推荐
- 桌面版Ubuntu系统固定IP设置和Network-manager设置
我自己的学校是绑定MAC和IP上网的.这种方法名义上说是为了安全,不过是个搞计算机的都知道随便修改一下MAC就可以使用别人的IP了. 不提蛋疼的事情了,先来说说网络的配置吧 我用的是Ubuntu14. ...
- Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure wit ...
- clnt_create: RPC: Program not registered
原因:[root@nfs nfs]# systemctl start nfs-utils 解决方法:[root@nfs nfs]# systemctl start nfs
- EGIT
https://jingyan.baidu.com/article/64d05a0262f013de55f73bcc.html
- Python3 tkinter基础 event keysym 查看按键的按键名
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 题解——HDU 4734 F(x) (数位DP)
这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...
- Kylin工作原理、体系架构
核心思想:预计算. 对多维分析可能用到的度量进行预计算,将计算好的结果保存成Cube,并存在HBase中,供查询时直接访问 将高复杂度的聚合运算.多表连接……操作转换成对预计算结果的查询.决定了Kyl ...
- Lintcode9-Fizz Buzz-Easy
Fizz Buzz Given number n. Print number from 1 to n. But: when number is divided by 3, print "fi ...
- React Naive 解决防止多次点击的解决方法
export default class TouchableOpacity extends Component { render() { return ( <TouchableOpacity a ...
- RN 使用第三方组件之react-native-image-picker(拍照/从相册获取图片)
首先给个github地址:https://github.com/react-community/react-native-image-picker 英文不行的看下面这个笔记 该插件可以同时给iOS和 ...