读中文TXT文件,再生成单个中文TXT文件,出现部分文件乱码。乱码概率在20%

Can_see_your_eye 2017-09-19 01:01:03
各位大神:
一个简单的程序,从某个中文txt逐个提取中文字符并逐一生成新的txt文件,每个新中文txt文件只写一个汉字字符。程序执行后,大部分中文汉字都可以从源txt生成新的中文txt,但是其中大概有20%的中文汉字生成了乱码。 请各位大神帮我看下!多谢!

下面是源中文txt文件。


下面是生成后的txt文件,每个文件都只对应一个汉字。 文件名称对应汉子的ask码。


有些汉字字符就会生成乱码:比如应该【匕】字变成了【ذ】,【匹】字变成了【ƥ】


运行编码我每个都是了UTF-8, UTF-16,US-ASK2更会出问题。


而且比较诡异的是,在程序运行中打印出的汉字都是正常的。


下面是2个class的程序源代码:

先是create

----------------------------------------------------------------------------------------------
package create2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Create_single {

public static void main(String args[]){

int ii=21268;
int ji=0;

//40869
while(ii<=21268)
{

FileReaderDemo tytyread= new FileReaderDemo();
String str="";
String path="";
String path2="";
String temp="";
String name=Integer.toString(ii);
char chartemp[]=new char[1];

//path="C:\\Users\\Administrator\\Desktop\\888\\changed\\ZZb_z_"+name+"_.java";

path="C:\\Users\\Administrator\\Desktop\\888\\line1\\ok"+name+".txt";

System.out.print((char)13+"PATH:["+path+"]");

str=tytyread.fileread2(path);

ji=0;
while(ji<str.length())
{
chartemp[0]=str.charAt(ji);
//temp="1"+String.copyValueOf(chartemp, 0, 1)+"2";
temp=str.charAt(ji)+" ";

System.out.print((char)13+"ji:["+ji+"]zi["+temp+"]"+"number["+(int)temp.charAt(0)+"]");


path2="C:\\Users\\Administrator\\Desktop\\888\\sigle\\ok"+(int)temp.charAt(0)+".java";

FileOutputStream fos=null;

try
{
fos=new FileOutputStream(path2);
}
catch(FileNotFoundException e)
{
System.out.println("file create faild");
}



byte buf[]=temp.getBytes();




try
{
fos.write(buf);
fos.close();
}
catch(IOException e)
{
e.printStackTrace();
}

buf=null;
fos=null;


ji++;
} //str while end

ii=ii+100;
} //while end


} // main end


}
----------------------------------------------------------------------------------------------

然后是 read

----------------------------------------------------------------------------------------------

package create2;
import java.io.*;



public class FileReaderDemo {






public String fileread2(String path)
{

String finaltext="";


String file_encoding= System.getProperty("file.encoding");
char[] texttemp=new char[12345678];



System.out.println("file type:"+file_encoding);
FileReader fr=null;


try
{
//fr=new FileReader("index1.java");
fr=new FileReader(path);
}
catch(FileNotFoundException e)
{
System.out.println("file read error at first charactor");
}



int c=0;
try{
int count=0;
while((c=fr.read())!=-1)
{
//System.out.print((char)c);


texttemp[count]=(char)c;
count++;

}


finaltext=String.copyValueOf(texttemp,0,count);


}
catch(IOException e)
{
System.out.println("file read error in step 2");
}
finally
{
try
{
fr.close();

}
catch(IOException e)
{
System.out.println("file close error");
}
}

//System.out.println("{"+finaltext+"}");
return finaltext;

}









}


----------------------------------------------------------------------------------------------

...全文
632 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TZYZlpx123456789 2018-01-09
  • 打赏
  • 举报
回复
兄弟你是啥解决方法,我这边也出现这个问题了 ,一半乱码一般正常!不知道什么原因,我也是写到TXT文件里面出现的
Can_see_your_eye 2017-09-19
  • 打赏
  • 举报
回复
引用 1 楼 qq_27762917 的回复:

public class Test {
	public static void main(String[] args) {
		String filePath = "D:/1.txt"; // txt文件路径
		String writePath = "D:/txt/"; // 要存入的文件夹路径
		write(filePath, writePath);
	}

	private static List<String> txtTransFormChineseStrs(String txtPath) {
		List<String> content = new ArrayList<String>();
		BufferedReader br = null;
		try {
			File file = new File(txtPath);
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
			br = new BufferedReader(isr);
			String lineTxt = null;
			while ((lineTxt = br.readLine()) != null)
				content.add(lineTxt);
		} catch (Exception e) {
			System.out.println("文件读取错误!");
			return null;
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
				System.out.println("无法关闭");
			}
		}
		List<String> chineseStrs = new ArrayList<String>();
		for (String str : content) {
			for (int i = 0; i < str.length(); i++)
				chineseStrs.add(str.substring(i, i + 1));
		}
		return chineseStrs;
	}

	private static void write(String txtPath, String writePath) {
		List<String> source = txtTransFormChineseStrs(txtPath);
		File file = null;
		BufferedWriter out = null;
		try {
			for (int i = 0; i < source.size(); i++) {
				file = new File(writePath + source.get(i) + ".txt");
				if (!file.exists())
					file.createNewFile();
				out = new BufferedWriter(new FileWriter(file));
				out.write(source.get(i));
				out.flush();
			}
		} catch (IOException e) {
			System.out.println("文件写入出错");
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				System.out.println("无法关闭");
			}
		}
	}
}
多谢! 我已经找到另外方法解决了..
Can_see_your_eye 2017-09-19
  • 打赏
  • 举报
回复
引用 2 楼 jkl012789 的回复:
应该是数组大小的问题,最后一个中文只读了一半,剩下的一半没读到,这就造成了乱码,要等到读取到特殊的标点符号,或者字母的时候,才会恢复成正常的中文,如果一直没读到标点符号 或者字母数字,那么后面的中文全部是乱码。
源文件中肯定没有标点符号. 而且只有20%的概率是乱码. 其他正常.
叮咚呛咚呛 2017-09-19
  • 打赏
  • 举报
回复
应该是数组大小的问题,最后一个中文只读了一半,剩下的一半没读到,这就造成了乱码,要等到读取到特殊的标点符号,或者字母的时候,才会恢复成正常的中文,如果一直没读到标点符号 或者字母数字,那么后面的中文全部是乱码。
Freefish1994 2017-09-19
  • 打赏
  • 举报
回复

public class Test {
	public static void main(String[] args) {
		String filePath = "D:/1.txt"; // txt文件路径
		String writePath = "D:/txt/"; // 要存入的文件夹路径
		write(filePath, writePath);
	}

	private static List<String> txtTransFormChineseStrs(String txtPath) {
		List<String> content = new ArrayList<String>();
		BufferedReader br = null;
		try {
			File file = new File(txtPath);
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
			br = new BufferedReader(isr);
			String lineTxt = null;
			while ((lineTxt = br.readLine()) != null)
				content.add(lineTxt);
		} catch (Exception e) {
			System.out.println("文件读取错误!");
			return null;
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
				System.out.println("无法关闭");
			}
		}
		List<String> chineseStrs = new ArrayList<String>();
		for (String str : content) {
			for (int i = 0; i < str.length(); i++)
				chineseStrs.add(str.substring(i, i + 1));
		}
		return chineseStrs;
	}

	private static void write(String txtPath, String writePath) {
		List<String> source = txtTransFormChineseStrs(txtPath);
		File file = null;
		BufferedWriter out = null;
		try {
			for (int i = 0; i < source.size(); i++) {
				file = new File(writePath + source.get(i) + ".txt");
				if (!file.exists())
					file.createNewFile();
				out = new BufferedWriter(new FileWriter(file));
				out.write(source.get(i));
				out.flush();
			}
		} catch (IOException e) {
			System.out.println("文件写入出错");
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				System.out.println("无法关闭");
			}
		}
	}
}

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧