- 论坛徽章:
- 0
|
理解流的概念是前提:
public static Vector getText(String strDirectory,String strFileName){
Vector vecResult = new Vector();
try{
FileReader fr = new FileReader(strDirectory+strFileName);
BufferedReader br = new BufferedReader(fr);
String strRecode ="";
while((strRecode = br.readLine()) != null){
vecResult.addElement(strRecode);
}
fr.close();
}catch (Exception e){
Object[] objMsg ={e.getMessage()};
JOptionPane.showMessageDialog(null,objMsg,"Exception",0);
e.printStackTrace();
return null;
}
return vecResult;
}
调用上边的 方法:
/**
* 受講情報ファイルを読み込む
*/
private boolean getStudentCsv() {
Vector vecRet = new Vector();
String strWork = "";
int intCnt1 = 0;
int intCnt2 = 0;
int intCnt3 = 0;
// File file;
// file = new
// File(this.getSystemDirectory(1)+this.getSystemFileName(1));
// if(file.isFile()==false){
// System.exit(0);//ファイルが存在しない場合、強制終了。
// Object[] objMsg ={pr_strMsg_002};
// JOptionPane.showMessageDialog(null,objMsg);
// return false;//ファイルが存在しない場合
// }
vecRet = commText.getText(this.getSystemDirectory(1), this
.getSystemFileName(1));
if (vecRet.isEmpty() == true)
return false;
strWork = (String) vecRet.get(0);
intCnt1 = strWork.indexOf(",");
if (intCnt1 == -1)
return false;// 受講者社員番号が存在しない場合
intCnt2 = strWork.indexOf(",", intCnt1 + 1);
if (intCnt2 == -1)
return false;// 受講者氏名が存在しない場合
intCnt3 = strWork.indexOf(",", intCnt2 + 1);
if (intCnt3 == -1)
return false;// 講習会IDが存在しない場合
p_str_EmployeeNo_ = strWork.substring(0, intCnt1);// 受講情報ファイルの社員番号
p_str_EmployeeName_ = strWork.substring(intCnt1 + 1, intCnt2);// 受講者情報ファイルの受講者氏名
p_str_ScheduleID_ = strWork.substring(intCnt2 + 1, intCnt3);// 受講情報ファイルの講習会ID
return true;
}
简单的读入:
import java.io.*;
public class test1 {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String wang = "wangchong";
byte buf[] = wang.getBytes();
FileOutputStream f = new FileOutputStream("C:\\file1.txt");
f.write(buf);
f.close();
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40671/showart_361334.html |
|