Java资源清册-如何使用FileInputStream读取文件?
在本学期的最后一个作业中,我必须创建一个包含项目对象数组的库存程序。每个项目都包含一个ID(在添加项目时分配,不能修改)、名称、说明、现有项目数量和单价 我还需要使用文件I/O流保存和加载文件。我可以很好地保存到文本文件中。但是,我在开始使用readFile方法时遇到了问题。我真的想在不寻求任何帮助的情况下完成这项任务,但我被难住了。如何使用FileInputStream读取文本文件 项目类别Java资源清册-如何使用FileInputStream读取文件?,java,fileinputstream,fileoutputstream,printwriter,inventory,Java,Fileinputstream,Fileoutputstream,Printwriter,Inventory,在本学期的最后一个作业中,我必须创建一个包含项目对象数组的库存程序。每个项目都包含一个ID(在添加项目时分配,不能修改)、名称、说明、现有项目数量和单价 我还需要使用文件I/O流保存和加载文件。我可以很好地保存到文本文件中。但是,我在开始使用readFile方法时遇到了问题。我真的想在不寻求任何帮助的情况下完成这项任务,但我被难住了。如何使用FileInputStream读取文本文件 项目类别 import java.text.NumberFormat; public class Item {
import java.text.NumberFormat;
public class Item
{
private int ID;
private String name;
private String Desc;
private int onHand;
private double unitPrice;
public Item(int pID)
{
ID = pID;
}
public Item(int pID, String pName, String pDesc, int pOnHand, Double pUnitPrice)
{
ID = pID;
name = pName;
Desc = pDesc;
onHand = pOnHand;
unitPrice = pUnitPrice;
}
public void display()
{
NumberFormat dollars = NumberFormat.getCurrencyInstance();
System.out.printf("%-6s%-20s%-24s%-12s%-6s\n", ID, name, Desc, onHand, dollars.format(unitPrice));
}
// GETTERS AND SETTERS
public int getID()
{
return ID;
}
public void setName(String pName)
{
name = pName;
}
public String getName()
{
return name;
}
public void setDesc(String pDesc)
{
Desc = pDesc;
}
public String getDesc()
{
return Desc;
}
public void setOnHand(int pOnHand)
{
onHand = pOnHand;
}
public int getOnHand()
{
return onHand;
}
public void setUnitPrice(double pUnitPrice)
{
unitPrice = pUnitPrice;
}
public double getUnitPrice()
{
return unitPrice;
}
}
存货类别
import java.util.Scanner;
import java.io.PrintWriter;
import java.io. FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Inventory
{
int max = 30;
int count = 0;
Item myItem[] = new Item[max];
Scanner scannerObject = new Scanner(System.in);
public void addItem()
{
try{
if (count >= max)
{
System.out.println("\nNo more room!");
}else{
System.out.print("\nPlease enter name of item: ");
String lname = scannerObject.nextLine();
System.out.print("\nPlease enter a brief description of the item: ");
String ldesc = scannerObject.nextLine();
System.out.print("\nPlease enter the amount on hand: ");
int lonHand = scannerObject.nextInt();
System.out.print("\nPlease enter unit price of the item: $");
Double lunitPrice = scannerObject.nextDouble();
myItem[count] = new Item(count + 1, lname, ldesc, lonHand, lunitPrice);
count++;
System.out.println("\nThank you. The ID number for " + lname + " is " + count);
scannerObject.nextLine();
}
}catch(Exception e)
{
System.out.println("\nERROR! Please try again:\n");
scannerObject.nextLine();
}
}
public int findItem()
{
int found = -1;
int inputID =0;
try{
System.out.print("\nGreetings, please enter the ID number for item:\n");
inputID = scannerObject.nextInt();
for(int i = 0; i < count; i++){
if(myItem[i].getID() == inputID){
found = i;
scannerObject.nextLine();
}
}
}catch(Exception e)
{
System.out.println("\nERROR!");
scannerObject.nextLine();
}
return found;
}
public void modify()
{
int lfound = findItem();
if (lfound == -1){
System.out.println("\nInvalid input! Please try again:");
scannerObject.nextLine();
}else{
try{
System.out.print("\nPlease enter name of item: ");
String lname = scannerObject.nextLine();
myItem[lfound].setName(lname);
System.out.print("\nPlease enter a brief description of the item: ");
String ldesc = scannerObject.nextLine();
myItem[lfound].setDesc(ldesc);
System.out.print("\nPlease enter the amount on hand: ");
int lonHand = scannerObject.nextInt();
myItem[lfound].setOnHand(lonHand);
System.out.print("\nPlease enter unit price of the item: $");
double lunitPrice = scannerObject.nextDouble();
myItem[lfound].setUnitPrice(lunitPrice);
scannerObject.nextLine();
}catch (Exception e)
{
System.out.println("\nInvalid command! Please try again: ");
scannerObject.nextLine();
}
}
}
public void displayAll()
{ System.out.println("_______________________________________________________________________________\n");
System.out.println(" Inventory ");
System.out.println("_______________________________________________________________________________\n");
System.out.printf("\n%-6s%-20s%-24s%-12s%-6s\n", "ID:", "Name:", "Description:","On Hand:", "Unit Price:\n"); //Header
System.out.println("_______________________________________________________________________________\n");
for(int i = 0; i < count; i++){
myItem[i].display();
}
}
public void displayOne()
{
int lfound = findItem();
if (lfound == -1){
System.out.println("\nInvalid input! Please try again:");
}else{
System.out.println("_______________________________________________________________________________\n");
System.out.println(" Inventory ");
System.out.println("_______________________________________________________________________________\n");
System.out.printf("\n%-6s%-20s%-24s%-12s%-6s\n", "ID:", "Name:", "Description:","On Hand:", "Unit Price:\n"); //Header
System.out.println("_______________________________________________________________________________\n");
myItem[lfound].display();
}
}
public void saveFile()
{
PrintWriter outputStream = null;
try{
outputStream =
new PrintWriter(new FileOutputStream("H:\\Java\\saveFile.txt"));
}catch (FileNotFoundException e)
{
System.out.println("Error!");
}
if(outputStream != null)
for(int i = 0; i < count; i++){
outputStream.println(myItem[i].getID());
outputStream.println(myItem[i].getOnHand());
outputStream.println(myItem[i].getUnitPrice());
outputStream.println(myItem[i].getName());
outputStream.println(myItem[i].getDesc());
}
outputStream.close();
}
}
根据我的指导老师的说法,我需要在库存类中使用保存和读取文件的方法。我需要在我的用户类中调用它们。虽然我的Item ID变量有一个“getter”,但不允许使用“setter”
我对Java还是相当陌生的,所以请原谅任何新手的错误。再次感谢您的帮助!我查了一下我的书,用谷歌搜索了一些例子,但我找不到任何与我的情况相关的东西 要使用FileInputStream读取文件,只需使用:
Scanner input = new Scanner(new FileInputStream("path_to_file"));
使用扫描仪读取数据的方法
while(input.hasNextLine()) { //or hasNextInt() or whatever you need to extract
input.nextLine() //... read in a line of text from the file
}
如果希望使用文件类方法执行任何文件操作,也可以使用文件类
File myTextFile = new File("path_to_file");
Scanner input = new Scanner(new FileInputStream(myTextFile));
当然,您需要捕获FileNotFoundException
否则,这和你为PrintWriter所做的一样。
只需将FileOutputStream
切换为FileInputStream
,将PrintWriter
切换为Scanner
,但不要忘记在从文件写入或读取切换时先关闭文件:
input.close() // or output.close()
我建议您使用分隔符,如
、
或空格来分隔文本文件中的数据(用于单个用户的信息)。然后使用新行分隔每个用户的条目。很好!我实际上正在考虑这样做,但不确定。然后您可以使用scanner类中的usedimiter()
方法。好的,我知道您在做什么。但是,我如何将这些分配给适当的变量呢?例如,将ID#分配给ID变量,将Name分配给Name变量等等?在从文本文件读回数据时,您很可能会创建项的新实例,将从文本文件检索到的值传递到构造函数或使用适当的setter方法。嗯,您可以说newitem(input.nextInt(),input.next(),input.next(),input.nextInt(),input.nextDouble());但要做到这一点,您应该确保文本文件中的每一项都在单独的一行中,每一条数据都按照构造器的正确顺序排列(或者让构造器按照文本文件中的正确顺序排列)
input.close() // or output.close()