qt5.5 读写文件的中文乱码解决方案

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tiandyoin/article/details/80525699
#include<QString>
#include<QSetting>

QString localToGBK(QString src)
{
	QString source = src;
	int len = source.length();
	if (len%2 == 1)
		source.append('a');
	QString qstr = QString::fromLocal8Bit(source.toStdString().c_str());
	QString retStr = QTextCodec::codecForName("gbk")->fromUnicode(qstr);
	if (len%2 == 1)
		retStr = retStr.left(retStr.length() -1);
	return retStr;
}

void testIni()
{
	Qstring file = ".\\test.ini";
	Qstring section = "section1";
	{
		QSetting setting(file, QSetting::IniFormat);
		setting.setIniCodec(QTextCodec::codecForName("UTF-8"));
		
		QString s = "中文字符";
		setting.setValue(section1 + "/key", s);
	}
	{
		QSetting setting(file, QSetting::IniFormat);
		setting.setIniCodec(QTextCodec::codecForName("UTF-8"));
		
		QString sKey = setting.value(section + "/key").toString();
		
		QTextStream(stdout << localToGBK(sKey);
	}
}

// 项目配置默认编码UTF-8, 如果编码是UTF-8则添加

展开阅读全文

一个读写文件中文乱码???

06-14

<%@ page contentType="text/html; charset=GBK" %>rn<%@page import="java.io.*" %>rn<%@page import="java.sql.*" %>rn<%@page import="java.util.*" %>rn<%@page import ="hzzs.upload"%>rnrnrnrn rn rnrnrnrn rn rn rn rn rn rn<%rn String file;rn file =request.getParameter("filename");rn file = lhj.getStr(file);rn int ii = file.lastIndexOf("\\");rn if(ii < 0 ||ii >= file.length() - 1)rn ii = file.lastIndexOf("/");rn rnfile = file.substring(ii + 1);rnjava.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMddHHmmss");rnjava.util.Date date1 = new java.util.Date();rnString s_time = format.format(date1);rns_time=s_time+".out";rn//request.setCharacterEncoding("gb2312");rn //File upload1=new File("E:/Tomcat 5.0/webapps/invest/pic/"+data_dir+uploadFile1);rn String title1="";rn title1 =request.getParameter("biaoti1");rn if(title1!=null)rn title1=lhj.getStr(title1);rnrnrnString head = request.getParameter("head");rnString content = request.getParameter("content");rnString kind = request.getParameter("kind");rnif(kind==null) kind="";rnString table = request.getParameter("table");rnString type = request.getParameter("type");rnint tt = Integer.parseInt(type);rn//int tt = 1;rn//out.println(content);rn//out.println(type);rnhead = lhj.getStr(head);rn//content = lhj.getStr(content);rn//kind = lhj.getStr(kind);rnrn//out.println(head);rn//out.println(picfile);rn//out.println(content);rn//out.println(table);rn//out.println(uploadFile1);rn//out.println(uploadFile2);rn//out.println(uploadFile3);rnrn//字体rnrn//content = bofs+content+eofs;rnrnString time = lhj.getTime();rnif(file.trim().equalsIgnoreCase("")||file.trim().equalsIgnoreCase("null")||file==null)rn file="null";rnelsernString date =time.substring(0,10);rnfile = date+"/"+file;rnrnrntryrn request.setCharacterEncoding("gb2312");rn PrintWriter writer= new PrintWriter(new BufferedWriter(new FileWriter("E:\\Tomcat 5.0\\webapps\\invest\\doc\\"+s_time,true)));rn writer.write(content);rn writer.close();rn //out.println("已经把内容写入到C:\\"+s_time);rn out.println("已经把内容写入到E:\\Tomcat 5.0\\webapps\\invest\\doc\\"+s_time);rn //out.println(s_time);rnrncatch(Exception e)rnrn out.println(e);rn e.printStackTrace();rnrnrnString sql="insert into "+table+" (head,time,kind,pic,flag,filedir) values('"+head+"','"+time+"','"+kind+"','"+file+"',0,'"+s_time+"')";rnint re = 0;rnre=lhj.update(sql);rnif (re==0)rn out.print(" —— 数据录入出错! ——");%>rn rn <%rn elsern out.print(" —— 数据录入成功! ——");%>rn rn <%rn lhj.con_close();rn%>rn rn rnrnrnrnrnrnrn 论坛

没有更多推荐了,返回首页