//商品 Thing 类 public
c class Thing [ char
Name []= new
int
Number ;//4个字节 double Price ;//8个字节 int
count ;//4个字节所以保存一个商品要32个字节
public Thing ( String name , intnumber , doubleprice int count ){ if ( name . toCharArray O . length >8)
System . arraycopy ( name . toCharArrax 0.0, this . e ◇
char [8];//16个字节
长度大于8.只取前8个
else
System . arraycopy ( name . toCharArray 0.0, this . Name ,0, name toCharArray ,
0.length◇;//如果字符长度小于8.有几个填几个
this . Number = number ; this . Price = price ;
this . Count - count :
public String getNameO {
return new ! String ( this . Name );
public
voidsetName ( String name ){
if ( name .toCharArray0. length >8)
System . arraycopy ( name . toCharArrax 0.0, this . Name ,0.8); else
System . arraycepy ( name . toCharArtay ,0.0, this . Name , O , nametoCharArrax
0.length);
public
doublegetPriceO {
Price ;
return
public void setPrice ( doubleprice ){
this . Price = price :
public
int
getNumber0{ Number ;
return
public voidsetNumber ( int number ){ this . Number = number ;
public
int
return
getCountO {
Count :
public
void
this . Count = count ;
setCount ( int count ){
//操作 Work 类(包含输入、查询、更改操作) import javaio *
public
class Work { String
FileName ;
public Work ( String FileName ){ this . FileName = FileName ;
◇
//写第 n 条商品记录 public
void
RandomAccessFile rafw = new
rafw . seek ( n *32);//将位置指示器移到指定位置上,每个商品占32个字节 for ( inti =0; i <8; i ++)
rafw . wxiteChar ( th Name [ i ]]://写入商品名
rafw . wxitelnt ( th . Number );//写入商品号码
rafw . writeDouble ( thPrice );//写入价格
rafw . writelnt ( thCount );//写入商品库存量
WriteThing ( Thing th int n ) throwsException {
RandomAccessFile ( FileName . tw "):
rafw . close ]:
//输入商品信息 public
void readThing ( int n ) throws Exception { charthingname []= new
char [8];
Random AccessFile rafr = newRandomAccessFile ( FileName , r ");
rafr seek ( n *32);//将位置指示器移到指定位置上,每个商品占32个字节 for ( int i =0; i <8; i ++)
thingname [ i ]- rafr - readCharO ://读到 char 商品名 System . out . print ("商品名:");
System . out . println ( thingname ):
System . out . printin ("编号:"+ rafr readint0); System . out . printin ("价格:"+ rafr . read Double (); System . out . printin ("商品库存量:"+ rafr readintO ); rafr . close :
System . out printinO :
wv
//修改存入 n 号商品名字 public
cvoid
Random AccessFile rafw = new ] rafw . seek ( n *32);
char [] borrow = new
char [8];
if ( name .toCharArray0.length>8)
System . arraycopy ( name . toCharArrayO .0.borrow.0.8): else
System . arraycopy ( name . toCharArraxO .0, borrow ,0, na me toCharArcaxxO .
setThingName ( int n . String name ) throws Exception {
RandomAccessFile ( FileName ," rw ");
length );
for ( inti =0; i <8:i++)
taw - wziteChar ( borrow [ i ]): rafw . closeO :
//修改存入 n 号商品价格 publicvoid
RandomAccessFile rafy - newBandom ä ccsssEile ( EileName ." tx "):
rafw . seek ( n *32+8+4);//将位置指示器移到指定位置上,每个商品占24个字节,名字占8字节
rafwwriteDouble ( price ); rafw . closeO :
setThingPrice ( int n , double price ) throws Exception {
//修改存入 n 号商品库存量 publicvoid
dsetThingCount ( int n int count ) throws Exception {
RandomAccessFilerahw - newBandomAcceasFile ( EileNams ." tx "):
rafw . seek ( n *32+8+4+8];//将位置指示器移到指定位置上,每个商品占24个字节,名字占8字节
tafwwriteFloat ( count ): rafw .close0:
//test6_10
import javautil Scanner :
publicclasstest6_10{
public static voidmain ( String [] args ) throws Exception { Scanner scanner = newScanner ( System in ):
int number ;
String name :
Work work = new Work (" d :/ naruto . tst "); Thing [] thing ={
new
Thing ("立白洗衣粉",1,3.5,55), Thing ("编辑者林楚金",2,0,0), Thing ("舒肤佳",4,8.9,89),
Thing ("杜蕾丝超薄系列",5,1.2,444444)小;
new
new
new
for ( inti =0; icthing length ; i ++)
workwriteThing ( thing [ i ]. i ];
for ( int i =0; icthing length ; i ++) workreadThing ( i );
System . out printin ("请输入要查询商品编号"); number - scanner . nextintO :
workreadThing ( number -1);
System . out . println ("请输入要修改某一商品名字:编号和名字"); number = scanner . nextintO :
name = scanner . next ();
worksetT hingName ( number -1, name ); workreadThing ( number -1);