BufferedReader 파일 내용 읽 기 중국어 코드 문제 및 해결 방안
File file = new File("D:/1.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String tmpStr = null;
while ((tmpStr=br.readLine())!=null) {
byte[] tmpByte = tmpStr.getBytes(Charset.forName("GBK"));
String name = new String(tmpByte,0,10,Charset.forName("GBK"));
String birthday = new String(tmpByte,10, 8,Charset.forName("GBK"));
String sex = new String(tmpByte,18, 1,Charset.forName("GBK"));
System.out.println(name+";"+birthday+";"+sex);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
수 정 된 코드: 파일 흐름 을 가 져 올 때 파일 의 인 코딩 문자 집합 을 설정 해 야 분석 할 때 정상적으로 해석 할 수 있 습 니 다.
File file = new File("D:/1.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file),Charset.forName("GBK")));
String tmpStr = null;
while ((tmpStr=br.readLine())!=null) {
byte[] tmpByte = tmpStr.getBytes(Charset.forName("GBK"));
String name = new String(tmpByte,0,10,Charset.forName("GBK"));
String birthday = new String(tmpByte,10, 8,Charset.forName("GBK"));
String sex = new String(tmpByte,18, 1,Charset.forName("GBK"));
System.out.println(name+";"+birthday+";"+sex);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【eclipse】같은 파일을 2개 열고 싶다【에디터의 분할】「이런 것은 다른 클래스로 나누어야 한다!」라든지 있다고는 생각합니다만. 실제로 실무 속에서 프로그램을 쓰고 있으면, 이런 소스에 눈에 걸리는 일도 적지 않을까···. 그건 그렇고, 내 노트북에서 이렇게 보입니다 네...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.