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 ) {
}
}
}
}
}
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 ) {
}
}
}
}
}
解决方案 »
- 头大,有Html标签分页问题
- 什么原因让我看不到我加的两个JTable
- 关于在JEditorPane上实现画线
- com.enterprisedt.net.ftp.FTPClient中restart
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");这个类在哪个.jar文件中
- 这个问题不一般
- java中<<和>>运算符,请给几个在实际中应用例子。
- Visual Age for Java高手帮帮我!
- 在哪下j2sdkee,java.sun.com好慢有没有快点的地方
- 本地服务器已经收到了请求,怎样回复微信服务器并不发信息给客户端或者说不给客户端做任何提示(后面是调用了客服接口来发消息的)?
- 100分求一个程序,实现三角形或任意多边形的颜色填充算法
- 新手请进:Java入门!(Hello World!问题)(转贴)
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方法,一样的。
不如好好的看以下 io package
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 ) {
}
}
}
}
}
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());
}
} ///:~
/*
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);}
}
}
FileInputStream in=new FileInputStream(file);
byte[] a=new byte[1];
while(in.read()!=-1)
{
System.out.println(new String(a));
}
......
我现在要解决的问题是这样的:将围棋的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])