免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 806 | 回复: 0
打印 上一主题 下一主题

file的读入 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-16 15:14 |只看该作者 |倒序浏览
理解流的概念是前提:
  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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP