import java.nio.*;
import java.nio.channels.*;
import java.io.*;public class OpenFile { public static void main(String[] args) {
          String name = "abc.txt";
BufferedInputStream bis = null;
        File file = new File( name );        try {
            int remaining = (int)file.length( );
            int offset = 0;
            int status = 0;
            rc = new byte[ remaining ];            bis = new BufferedInputStream( new FileInputStream( file ) );            while( remaining > 0 && ( status = bis.read( rc, offset, remaining ) ) >= 0 ) {
                offset += status;
                remaining -= status;
            }
            doc.setContent( DataComponent.RAW, rc );        } catch( FileNotFoundException ex ) {
            throw new FileException( "file not found");
        } catch( Exception ex ) {
            throw new FileException("Unknown other exception" );
        }
        finally {
            if( bis != null ) {
                try {
                    bis.close( );
                } catch( Exception ex2 ) {
                }
            }
        }
}
}

解决方案 »

  1.   

    doc.setContent( DataComponent.RAW, rc );  -> System.out.println(rc);
      

  2.   


    File file = new File("1.txt");
             try {
          BufferedReader br = new BufferedReader(new FileReader(file));
          String tmp = null;
          try {
            while ( (tmp = br.readLine()) != null) {
              System.out.println(tmp);
            }
          }
          catch (IOException ex1) {
          }
             }
             catch (FileNotFoundException ex) {
             }
    读是这样的。写就是用Writer进行write方法,一样的。
      

  3.   

    只看别人给你copy的这些代码你还是没有自己做的能力
    不如好好的看以下 io package
      

  4.   

    Java的IO很强大,新版的java.nio.*这个包性能上又有很大提高。IO包中有很多针对stream处理的类和方法,组合变化也很多,常用的楼上也都提到了,最好自己再看看Java的I/O,对用好I/O很有好处。
      

  5.   

    import java.nio.*;
    import java.nio.channels.*;
    import java.io.*;public class OpenFile { public static void main(String[] args) {
              String name = "abc.txt";
    BufferedInputStream bis = null;
            File file = new File( name );        try {
                int remaining = (int)file.length( );
                int offset = 0;
                int status = 0;
                rc = new byte[ remaining ];            bis = new BufferedInputStream( new FileInputStream( file ) );            while( remaining > 0 && ( status = bis.read( rc, offset, remaining ) ) >= 0 ) {
                    offset += status;
                    remaining -= status;
                }
                doc.setContent( DataComponent.RAW, rc );        } catch( FileNotFoundException ex ) {
                throw new FileException( "file not found");
            } catch( Exception ex ) {
                throw new FileException("Unknown other exception" );
            }
            finally {
                if( bis != null ) {
                    try {
                        bis.close( );
                    } catch( Exception ex2 ) {
                    }
                }
            }
    }
    }
      

  6.   

    import java.io.*;public class IOBug {
      public static void main(String[] args) 
      throws Exception {
        DataOutputStream out =
          new DataOutputStream(
            new BufferedOutputStream(
              new FileOutputStream("Data.txt")));
        out.writeDouble(3.14159);
        out.writeBytes("That was the value of pi\n");
        out.writeBytes("This is pi/2:\n");
        out.writeDouble(3.14159/2);
        out.close();    DataInputStream in =
          new DataInputStream(
            new BufferedInputStream(
              new FileInputStream("Data.txt")));
        BufferedReader inbr =
          new BufferedReader(
            new InputStreamReader(in));
        // The doubles written BEFORE the line of text
        // read back correctly:
        System.out.println(in.readDouble());
        // Read the lines of text:
        System.out.println(inbr.readLine());
        System.out.println(inbr.readLine());
        // Trying to read the doubles after the line
        // produces an end-of-file exception:
        System.out.println(in.readDouble());
      }
    } ///:~
      

  7.   

    以下代码是一个读取一个文件,每次读一行,然后将读取的内容放到一个Vector中,再将Vector中的内容和行号一同写入另外一个文件。运行时采用 java BufferReadLine 参数1 参数2 参数3的方式运行!参数一为待读取的文件 参数二为写入的文件,参数三为在读取文件中查找指定的字符,会将包含指定字符的行的内容打印在屏幕上!学习用不错!
    /*   
    File: BufferReadLine.java
    Date: 03/03/2004
    */import java.io.*;
    import java.util.*;
    /**
     * @author  wei wang
     * @version 1.10, 03/03/2004
     * @since   JDK1.4.2
     */public class BufferReadLine
    {
        /**
         * open the specified file,read the content by row ,then put each row's content
         * into a Vector,at the same time search the then specified word in each row's content
     * if clude this word ,printf this row's content
     * write the content and row num into the specified file.
         * @param      fileRead   a file want to read.
     * @param      fileWrite   a file want to write.
     * @param      searchWord   a word want to serach from fileRead.
     * @exception  java.io.IOException if an I/O error occurs
         */  public void FileRW(String fileRead,String fileWrite,String searchWord) throws IOException
    {
    Vector vector=new Vector();
    String str=null;

    File fin=null;
    File fout=null;
    FileReader  fr=null;
    BufferedReader inbr=null;
    String keyWords=null;
    fin=new File(fileRead);
    keyWords=new String(searchWord);

    if(fin.exists() && fin.isFile() && fin.canRead())
    {
    fr = new FileReader (fin);
    inbr =new BufferedReader(fr);
    }
    else
    {
    System.err.println(fileRead+" not exists or it not a file or it can't read");
    System.exit(-1);
    }


    System.out.println("file "+fileRead+"'s content is following,which include the word \""+searchWord+"\"");
    System.out.println("------------------------------------------------------------------");
    do
    {
    str=inbr.readLine();
    if (str!=null){
    if(str.indexOf(keyWords)!=-1)
    System.out.println(str);
    vector.add(str);}
    }
    while(str!=null);
    //close file
    inbr.close();
    fr.close();
    fout=new File(fileWrite);
    //If the file is exists and can't write
    if (fout.exists() && !fout.canWrite())
    {
    System.err.println("you have no rights to write this file "+fileWrite);
    System.exit(-1);

    } FileOutputStream out = new FileOutputStream(fout); 
    PrintStream p = new PrintStream(out);

    for(int i=0;i<vector.size();i++)
    p.println(String.valueOf(i+1)+'\t'+vector.get(i));
    p.close();
    out.close();

    } public static void main(String[] args)
    {
    if(args.length!=3){
    System.err.println("use: java Main <fileNameRead> <fileNameWrite> <Searchwords>");
    System.exit(-1);}
    BufferReadLine filedemo=new BufferReadLine();
    try{
    filedemo.FileRW(args[0],args[1],args[2]);}
    catch(IOException e){
    System.out.println("file read or write error");
    System.exit(-1);}
    }
    }
      

  8.   

    File file=new File("..txt");
    FileInputStream in=new FileInputStream(file);
    byte[] a=new byte[1];
    while(in.read()!=-1)
    {
       System.out.println(new String(a));
    }
    ......
      

  9.   

    感谢各位:
    我现在要解决的问题是这样的:将围棋的SGF文件(棋谱)内容导入到数据库中:(很有现实意义)
    SGF文件是通用棋类文件,也是文本文件:
    例如下面就是一个SGF文件:
    以“EV”开头的是棋战名称,“DT”开头的是对局日期......
    我设计的数据库中将分别存储下列信息,
    就要求程序中先读取这个文本文件(也许会是若干个),再识别“EV”,“DT”等关键字,再将其后的内容放到数据库中,
    希望有高人给出详细代码,我将另有高分相送。(;EV[韩国第44届国手挑战者决定赛]DT[2001-01-09]PC[韩国]PB[李昌镐]BR[九段]PW[曹薰铉]WR[九段]KM[6.5]RE[W+R]US[棋圣道场]SO[http://weiqi.cn.tom.com/]SZ[19]C[http://weiqi.cn.tom.com
    棋圣道场-->精彩时局];B[pd];W[dd];B[qp];W[dq];B[do]
    ;W[co];B[dp];W[cp];B[eq];W[cn];B[dn];W[dm];B[cq];W[dr];B[bq];W[cr];B[cm];W[cl]
    ;B[br];W[er];B[bm];W[bn];B[bl];W[bp];B[fq];W[fo];B[ap];W[ao];B[fr];W[bs];B[ck]
    ;W[dl];B[en];W[dk];B[cj];W[fm];B[fn];W[gn];B[go];W[em];B[fp];W[ho];B[gp];W[hn]
    ;B[iq];W[dh];B[dj];W[ej];B[ch];W[cg];B[bg];W[cf];B[ei];W[di];B[ci];W[bf];B[ah]
    ;W[fi];B[jc];W[gc];B[nd];W[oq];B[pq];W[op];B[po];W[je];B[oo];W[jq];B[ip];W[no]
    ;B[nn];W[mo];B[mn];W[ln];B[lm];W[lo];B[ml];W[qi];B[qk];W[rk];B[rj];W[qj];B[ri]
    ;W[qh];B[rh];W[qg];B[ql];W[mh];B[ke];W[kf];B[kd];W[rl];B[rm];W[qc];B[qd];W[pb]
    ;B[jf];W[oc];B[od];W[rd];B[re];W[rc];B[jr];W[eh];B[jg];W[rf];B[cs];W[ds];B[ar]
    ;W[aq];B[dc];W[ec];B[ap];W[mc];B[oh];W[og];B[ng];W[of];B[qf];W[pf];B[qe];W[rg]
    ;B[nc];W[nb];B[mb];W[lb];B[md];W[lc];B[nf];W[nh];B[oi];W[kh];B[lg];W[oj];B[ni]
    ;W[lh];B[mj];W[kg])
      

  10.   

    可以考虑一下逐byte读取和StringTokenizer,新一点的方法用Regular Expression也不错。
      

  11.   

    最好是用xml来表示,用正则表达式也可以