New IO 读写文件,编码出现问题!

最近在做一个FileUtil,技术采用New IO, 在做的时候出现了编码问题!
例如:
我采用writeFile("D:\test.txt","中国",null)
然后我用readFile("D:\test.txt")读结果就会返回乱码!
后来我用Charset解码 , 获取目标文件编码(System.getProperty("file.encoding")),但是还是不行.
估计我应该要获得目标文件字节流的编码,这样才能根据相应的编码去读文件.
我怎么样才能判断目标文件的字节流编码呢?
或许我们会有更好的办法.请各位指教?谢谢!


// 读文件采用的字符编码.
private static Charset charset = Charset.forName(System.getProperty("file.encoding"));
/**
* 读文件
* @param fileName
* @return 读入的字符串
*/
public String readFile(String fileName){
CharBuffer cb = null;
try {
FileChannel in = new FileInputStream(fileName).getChannel();
int size = (int)in.size();
MappedByteBuffer mppedByteBuffer = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
cb = charset.newDecoder().decode(mppedByteBuffer);
in.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
return cb.toString();
}

/**
* 创建文件(写文件)
* @param fileName 文件名
* @param content 内容
* @param encoding 编码 (就是你需要以哪一种编码格式进行写入) . 默认采用(utf-8).
* @return true 创建成功 ,false 创建失败
*/
public boolean writeFile(String fileName,String content,String encoding){
try{
FileChannel out = new FileOutputStream(fileName).getChannel();
encoding = encoding == null ? "" : encoding;
if(encoding.length() <= 0)
encoding = "utf-8";
out.write(ByteBuffer.wrap(content.getBytes(encoding)));
out.close();
return true;
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return false;
}

读写文件出现问题

08-26

我把关键的代码贴出来rn[code=C/C++]rnAddnewDlg.cpprn// AddnewDlg.cpp : 实现文件rn//rnrn#include "stdafx.h"rn#include "Bookmanage.h"rn#include "AddnewDlg.h"rn#include "afxdialogex.h"rn#include "BookMessage.h"rnrnrn// CAddnewDlg 对话框rnrnIMPLEMENT_DYNAMIC(CAddnewDlg, CDialogEx)rnrnCAddnewDlg::CAddnewDlg(CWnd* pParent /*=NULL*/)rn : CDialogEx(CAddnewDlg::IDD, pParent)rn , bookid(0)rn , publishid(0)rn , authoroneid(0)rn , authortwoid(0)rn , authorthreeid(0)rn , bookname(_T(""))rn , publishname(_T(""))rn , authoronename(_T(""))rn , authortwoname(_T(""))rn , authorthreename(_T(""))rnrnrnrnrnCAddnewDlg::~CAddnewDlg()rnrnrnrnvoid CAddnewDlg::DoDataExchange(CDataExchange* pDX)rnrn CDialogEx::DoDataExchange(pDX);rn DDX_Text(pDX, IDC_EDIT1, bookid);rn DDX_Text(pDX, IDC_EDIT3, publishid);rn DDX_Text(pDX, IDC_EDIT6, authoroneid);rn DDX_Text(pDX, IDC_EDIT5, authortwoid);rn DDX_Text(pDX, IDC_EDIT7, authorthreeid);rn DDX_Text(pDX, IDC_EDIT2, bookname);rn DDX_Text(pDX, IDC_EDIT4, publishname);rn DDX_Text(pDX, IDC_EDIT8, authoronename);rn DDX_Text(pDX, IDC_EDIT10, authortwoname);rn DDX_Text(pDX, IDC_EDIT9, authorthreename);rnrnrnrnBEGIN_MESSAGE_MAP(CAddnewDlg, CDialogEx)rn ON_BN_CLICKED(IDC_BUTTON1, &CAddnewDlg::OnBnClickedButton1)rnEND_MESSAGE_MAP()rnrnrn// CAddnewDlg 消息处理程序rnrnrnvoid CAddnewDlg::OnBnClickedButton1()rnrn // TODO: 在此添加控件通知处理程序代码rn UpdateData(true);rn Book book;//创建Book对象进行添加rn Book temp;//创建空对象写入文本末尾rn fstream Hfdat("bookFile.dat",ios::in|ios::out|ios::binary );//以读、写方式打开rn Hfdat.seekg(0,ios::beg);rn dorn rn Hfdat.read((char *)&book,sizeof(Book)); //读一个记录rn while(!endmark(book)); //判断是否结束标志rn Hfdat.seekp(-long(sizeof(Book)),ios::cur); //写指针位置rn book.bookID=bookid;rn book.bookName=bookname;rn book.publishID=publishid;rn book.publishName=publishname;rn book.remainder=1;rn book.total=1;rn book.author1.authorID=authoroneid;rn book.author1.authorName=authoronename;rn if(authortwoid!=0)rn book.author2.authorID=authortwoid;rn book.author2.authorName=authortwoname;rn rn if(authorthreeid!=0)rn book.author3.authorID=authorthreeid;rn book.author3.authorName=authorthreename;rn rn Hfdat.write((char *)&book,sizeof(Book));rn Hfdat.write((char *)&temp,sizeof(Book));rn Hfdat.close();rn MessageBox(L"添加成功",L"提示");rn OnOK();rnrnrn[/code]rnrnrn[code=C/C++]rnrnAddDlg.cpprn// AddDlg.cpp : 实现文件rn//rnrn#include "stdafx.h"rn#include "Bookmanage.h"rn#include "AddDlg.h"rn#include "afxdialogex.h"rn#include "BookMessage.h"rn#include "AddnewDlg.h"rnrnrn// CAddDlg 对话框rnrnIMPLEMENT_DYNAMIC(CAddDlg, CDialogEx)rnrnCAddDlg::CAddDlg(CWnd* pParent /*=NULL*/)rn : CDialogEx(CAddDlg::IDD, pParent)rn , bookid(0)rnrnrnrnrnCAddDlg::~CAddDlg()rnrnrnrnvoid CAddDlg::DoDataExchange(CDataExchange* pDX)rnrn CDialogEx::DoDataExchange(pDX);rn DDX_Text(pDX, IDC_EDIT1, bookid);rnrnrnrnBEGIN_MESSAGE_MAP(CAddDlg, CDialogEx)rn ON_BN_CLICKED(IDC_BUTTON1, &CAddDlg::OnBnClickedButton1)rnEND_MESSAGE_MAP()rnrnrn// CAddDlg 消息处理程序rnrnrnrnrnvoid CAddDlg::OnBnClickedButton1()rnrn // TODO: 在此添加控件通知处理程序代码rn UpdateData(true);rn fstream Hfdat("bookFile.dat",ios::in|ios::out|ios::binary );//以读、写方式打开rn Hfdat.seekg(0,ios::beg); //2进制数据文件读指针移到文件头rn Book book;rn dorn rn Hfdat.read((char *)&book,sizeof(Book)); //读一个数据rn while(book.bookID!=bookid&&!endmark(book)); //判断是否找到记录rn if(book.bookID==bookid)rn rn rn book.remainder++;rn book.total++;rn Hfdat.seekp(-long(sizeof(Book)),ios::cur);rn Hfdat.write((char *)&book,sizeof(Book));rn Hfdat.close();rn MessageBox(L"已有书籍,增加库存量成功",L"提示");rn OnOK();rn rn elsern rn Hfdat.close();rn MessageBox(L"新书籍,请重新填写书籍信息",L"提示");rn OnOK();rn CAddnewDlg win;rn win.DoModal();rn rn rnrn[/code]rnrn[code=C/C++]rnrnBookMessage.hrn//书籍类rn#include "afx.h"rn#includern#includernusing namespace std;rnrnclass Authorrnpublic:rn int authorID;//作者ID号rn CString authorName;//作者姓名rn rnrn;rnrnclass Bookrnpublic:rn int bookID;//图书号rn CString bookName;//图书名rn int publishID;//出版社IDrn CString publishName;//出版社名rn int total;//总库存量rn int remainder;//现存量rn Author author1;//作者一信息rn Author author2;//作者二信息rn Author author3;//作者三信息rn rn //构造函数rn Book()rn bookID=0;rn bookName="";rn publishID=0;rn publishName="";rn total=0;rn remainder=0;rn author1.authorID=0;rn author1.authorName="";rn author2.authorID=0;rn author2.authorName="";rn author3.authorID=0;rn author3.authorName="";rn rnrn rn rn;rnclass Booklendrnpublic:rn int bookID;//图书号rn int LendID;//借阅者IDrn int year;//归还期限的年rn int month;//归还期限的月rn int day;//归还期限的日rn Booklend()rn rn bookID=0;rn LendID=0;rn year=0;rn month=0;rn day=0;rn rn rnrn;rnrnrnint endmark(Book); //判断是否到达文件尾[/code]rnrnrnrn[code=C/C++]//endmark.cpprn//判断是否录入空白纪录rn#include "StdAfx.h"rnrn#include"BookMessage.h"rnint endmark(Book book)rnrn if(book.bookID==0)rn return 1;rn return 0;rnrn[/code]rnrn为什么每次我一添加成功以后好像二进制文件就坏了,等我下一次操作的时候直接程序无法运行下去,在线等,急急急!

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试