当前位置:编程学习 > JAVA >>

新手求教关于文件读取的...

想编辑一个函数用来实现从文件中读取数据后显示在jFrame的表格中,我先定义了一个类来声明变量:
class data{
        String num;
        String name;
}
然后新建一个函数来从文件中读取数据:

private void show(){
data a[];
a = new data[10];
FileInputStream fis=new  FileInputStream("数据.txt");
DataInputStream indata=new DataInputStream(fis);        
for(int i=0;i<10;i++){
   a[i].num=indata.readUTF();
   a[i].name=indata.readUTF();
}
 for(int i=0;i<10;i++){
   jTable1.setValueAt(a[i].num, i+1, 1);
   jTable1.setValueAt(a[i].name, i+1, 2);
}
我是用netbeans的可视化直接创建的表格,监视器和内容都是自动定义好的。
请问我这样做对不对?我自己测试了下,没有在表格中显示,好像都没读取到,错的话应该怎么改?,应该用什么数据流,谢谢了
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- File file = new File("D:/text.txt");

try {

BufferedReader reader = new BufferedReader(new FileReader(file));

String line = null;

while ((line = reader.readLine()) != null) {

System.out.println(line);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} --------------------编程问答-------------------- 我想问下就是我建立的数组,比方a[0].num记录数字,a[0].name记录名字,下一组用a[1]等等,怎么从如图文件中读取并准确放到各个变量中? --------------------编程问答-------------------- /**
     * @param path 文件路径
     * @param fgf  分隔符
     * @return
     */
public static Map<String,String> readFile(String path,String fgf)
    {
        Map<String,String> map = new HashMap<String, String>();
        File file = new File(path);
        BufferedReader reader = null;
        try {
         reader = new BufferedReader(new FileReader(file));
         String tempString = null;
         //一次读入一行,直到读入null为文件结束
         while ((tempString = reader.readLine()) != null) {       
             String [] tempAry = tempString.split(fgf);
             map.put(tempAry[0], tempAry[1]);
         }
         reader.close();
        } catch (IOException e) {
         e.printStackTrace();
        } finally {
         if (reader != null) {
          try {
           reader.close();
          } catch (IOException e1) {
          }
         }
        }
           
         return map;
    }

readFile("路径。。。"," "); --------------------编程问答-------------------- 如果lz用java bean类来保存数据的话,建议还是把getter,setter写上。还有,你那个数据没有显示出来,你确定数据设置完之后有repaint。或者你是在初始化的时候加载的数据?
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,