关于java输入输出流的问题

mmbulaila 2011-04-20 10:09:25
我先创建了一个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();
}
我想问的是为什么我输入的数据没问题,读出来的数据的位置有偏差,而且是乱码。
...全文
315 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmbulaila 2011-04-25
  • 打赏
  • 举报
回复
import java.io.RandomAccessFile;
import java.io.IOException;
public class Tool {
public static final int SIZE = 72;
private int recordnumber;
private String Toolname;
private int Quanlity;
private double Cost;
public Tool()
{
this ( 0, "", 0, 0.0 );
}
public Tool(int acc,String name,int num,double cost)
{
Setrecorder(acc);
Setname(name);
SetQuanlity(num);
Setcost(cost);
}
public void Setrecorder(int acc)
{
recordnumber=acc;
}
public void Setname(String name)
{
Toolname=name;
}
public void SetQuanlity(int num)
{
Quanlity=num;
}
public void Setcost(double cost)
{
Cost=cost;
}
public int getrecord()
{
return recordnumber;
}
public String getname()
{
return Toolname;
}
public int getnum()
{
return Quanlity;
}
public double getcost()
{
return Cost;
}
public void read( RandomAccessFile file ) throws IOException
{
Setrecorder( file.readInt() );
Setname( readName( file ) );
SetQuanlity( file.readInt() );
Setcost( file.readDouble() );
}
private String readName( RandomAccessFile file ) throws IOException
{
char name[] = new char[ 15 ], temp;

for ( int count = 0; count < name.length; count++ )
{
temp = file.readChar();
name[ count ] = temp;
}

return new String( name ).replace( '\0', ' ' );
}
private void writeName( RandomAccessFile file, String name )
throws IOException
{
StringBuffer buffer = null;

if ( name != null )
buffer = new StringBuffer( name );
else
buffer = new StringBuffer( 15 );

buffer.setLength( 15 );
file.writeChars( buffer.toString() );
}
public void write( RandomAccessFile file ) throws IOException
{
file.writeInt( getrecord() );
writeName( file, getname() );
file.writeInt( getnum() );
file.writeDouble( getcost() );
}
}
lang_love_java 2011-04-22
  • 打赏
  • 举报
回复
能不能给Tool类?
mmbulaila 2011-04-22
  • 打赏
  • 举报
回复
这是完整的3段代码,没有错误,只是我读入数据时一切正常,读取出数据时都是乱码而且数据部对应,请java高手么看看吧,我新手,什么都不会,指点一下,谢谢了,不然真的什么都不会。

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧