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 ...
随机推荐
- Jenkins 总结
步骤: 1,安装Jenkins 2,运行Jenkins: java -jar jenkins.war --httpPort=8888 httpPort指的就是Jenkins所使用的http端口,这里指 ...
- topcoder srm 690 div1 -3
1.给定一个数字$N$,从1到100的100个数字中选出$K$个数字(设为集合$S$),然后对$S$进行如下运算: (1)删除$S$中的某些数字:(可以删除0个数字) (2)将$S$中的某些数字取为它 ...
- Java TreeSet的定制排序
注:只贴出实现类 package Test3; import java.util.Comparator;import java.util.TreeSet; public class Test { pu ...
- SpringBoot 解决HttpServletRequest只能读取一次
业务逻辑,通过filter读取请求的request,获取token,并将token传递后面流程使用 BodyReaderHttpServletRequestWrapper: public class ...
- JAVA JDBC 增删改查简单例子
1.数据库配置文件jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username= ...
- MySQL 安装步骤
今天用了一下MySQL,刚好看到之前电保存脑的笔记,于是整理了一下,还是记在博客上方便查询. 1.官网下载https://dev.mysql.com/downloads/mysql/之前安装的是mys ...
- 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)
题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...
- 论文笔记:Tracking by Natural Language Specification
Tracking by Natural Language Specification 2018-04-27 15:16:13 Paper: http://openaccess.thecvf.com/ ...
- Dockerize a .NET Core application
Dockerize a .NET Core application Introduction This example demonstrates how to dockerize an ASP.NET ...
- 微服务架构与实践4_Docker
构建Docker映像 定义Dockerfile=>Docker根据Dockerfile构建出映像 包含: 基础映像(父映像)信息 维护者信息 映像操作命令 容器启动命令 .net standar ...