关于java输入输出流的问题
我先创建了一个100个对象的控件的dat文件:
import java.io.IOException;
import java.io.RandomAccessFile;
public class CreatList {
private static final int NUMBER_RECORDS = 100;
public void createFile()
{
RandomAccessFile file = null;
try
{
file = new RandomAccessFile( "hardware.dat", "rw" );
Tool blankRecord =new Tool();
for ( int count = 0 ; count < NUMBER_RECORDS; count++ )
blankRecord.write( file );
System.out.println( "Created file clients.dat." );
}
catch ( IOException ioException )
{
System.err.println( "Error processing file." );
System.exit( 1 );
}
finally
{
try
{
if ( file != null )
file.close();
}
catch ( IOException ioException )
{
System.err.println( "Error closing file." );
System.exit( 1 );
}
}
}
}
然后调用自己写的一个读入的类:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Writelist {
private RandomAccessFile output;
private static final int NUMBER_RECORDS = 100;
public void openFile()
{
try
{
output = new RandomAccessFile( "hardware.dat", "rw" );
}
catch ( IOException ioException )
{
System.err.println( "File does not exist." );
}
}
public void closeFile()
{
try
{
if ( output != null )
output.close();
}
catch ( IOException ioException )
{
System.err.println( "Error closing file." );
System.exit( 1 );
}
}
public void addRecords()
{
Tool record = new Tool();
int recordnumber = 0 ; //产品的编号
String Toolname; // 产品的名字
int Quanlity; // 产品个数
double balance; // 产品单价
Scanner input = new Scanner( System.in );
System.out.printf( "%s\n%s\n%s\n%s\n\n",
"To terminate input, type the end-of-file indicator ",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter" );
System.out.printf( "%s %s\n%s", "Enter tool number (1-100),",
"Toolname, quanlity and cost", "? " );
while ( input.hasNext() )
{
try
{
recordnumber = input.nextInt(); // read account number
Toolname = input.next(); // read first name
Quanlity = input.nextInt(); // read last name
balance = input.nextDouble(); // read balance
if ( recordnumber > 0 && recordnumber <= NUMBER_RECORDS )
{
record.Setrecorder( recordnumber );
record.Setname(Toolname);
record.SetQuanlity(Quanlity);
record.Setcost( balance );
output.seek( (recordnumber - 1 ) *
Tool.SIZE );
record.write( output );
}
else
System.out.println( "Record Number must be between 0 and 100." );
}
catch ( IOException ioException )
{
System.err.println( "Error writing to file." );
return;
}
catch ( NoSuchElementException elementException )
{
System.err.println( "Invalid input. Please try again." );
input.nextLine();
}
System.out.printf( "%s %s\n%s", "Enter account number (1-100),",
"Toolname, Quanlity and balance.", "? " );
}
}
}
还有写入的类:
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadList {
private RandomAccessFile input;
public void openFile()
{
try
{
input = new RandomAccessFile( "hardware.dat", "r" );
}
catch ( IOException ioException )
{
System.err.println( "File does not exist." );
}
}
public void readRecords()
{
Tool record = new Tool();
System.out.printf( "\n%-10s%-15s%-15s%10s\n", "RecordNum",
"Toolname", "Quanlity", "Cost" );
try
{
int i=0;
while ( i<100 )
{
record.read( input );
if ( record.getrecord() != 0 )
System.out.printf( "%-20d%-12s%-12s%10.2f\n",
record.getrecord(), record.getname(),
record.getnum(), record.getcost() );
++i;
}
}
catch ( EOFException eofException )
{
return;
}
catch ( IOException ioException )
{
System.err.println( "Error reading file." );
System.exit( 1 );
}
}
public void closeFile()
{
try
{
if ( input != null )
input.close();
}
catch ( IOException ioException )
{
System.err.println( "Error closing file." );
System.exit( 1 );
}
}
}
我的main函数是这样写的:
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CreatList s1=new CreatList();
s1.createFile();
Writelist s2=new Writelist();
s2.openFile();
s2.addRecords();
s2.closeFile();
ReadList s4=new ReadList();
s4.openFile();
s4.readRecords();
s4.closeFile();
}
我想问的是为什么我输入的数据没问题,读出来的数据的位置有偏差,而且是乱码。