从文件中读取数据
byte buffer[]=new byte[10];
FileInputStream in = new FileInputStream("abcd.txt");if ( in != null )
{
in.read( buffer );
String str = new String( buffer,0 );
//String str = buffer.toString();
System.out.println( "read file data:" + str);
in.close();
}
-------------------------------------
char buffer[] = new char[256];
FileReader fileReader = new FileReader( "abcd.txt" );
int readnum = fileReader.read( buffer );
String str = new String( buffer,0,readnum );
System.out.println( "read file data:"+str );
fileReader.close();
}
-------------------------------------
写入数据到文件中
-------------------------------------
byte[] somebyte = {'a','b','c'};
String hello = "hello";
FileOutputStream out = new FileOutputStream("abcd.txt");
if ( out != null )
{
out.write( somebyte );
String str = new String( somebyte,0 );
for ( int i = 0;i<hello.length();i++ )
out.write( hello.charAt(i) );
out.close();
}