当它尝试为 Build 函数中的文件创建扫描仪时,我得到一个错误。创建文件后,它具有路径名作为文件,但是当扫描仪尝试使用该文件时,它找不到所述文件。两个扫描仪都失败了。
我刚刚开始在这里工作,在此之前它工作得很好,我没有改变构建方法或扫描部分只是为了调试,所以我知道那仍然是一样的。(第一个构建方法删除了调试代码)。
***如果人们想要该文件,我会发布一张图片,位于“C:\Users\User\Documents\”中,我知道我会被问到该文件是否存在。
import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CLInventorizerMaster extends Applet implements ActionListener
{
/**
* Version 1.0
*/
private static final long serialVersionUID = 1L;
Runtime rt = null;
//Data
int m_iNext = 0;
int m_iInventorySize = 2000;
static String m_sInventoryPath = null;
static String m_sMarkedItemsPath = null;
String m_saInventory[] = new String[m_iInventorySize];
String m_saNumber[] = new String[m_iInventorySize];
String m_saDescription[] = new String[m_iInventorySize];
String m_saRetail[] = new String[m_iInventorySize];
String m_saDiscount[] = new String[m_iInventorySize];
String m_saPrice[] = new String[m_iInventorySize];
String m_saMarkedItems[] = new String[m_iInventorySize];
String m_saActive[] = new String[m_iInventorySize];
// Visual
Button m_BtnLoadInventory = null;
Button m_BtnLoadItems = null;
TextField m_TxtLoadInventory = null;
TextField m_TxtLoadItems = null;
TextField m_TxtDescription = null;
TextField m_TxtItemNumber = null;
TextField m_TxtRetail = null;
TextField m_TxtDiscount = null;
TextField m_TxtPrice = null;
TextArea m_TxtaOut = null;
Button m_BtnNext = null;
public void init()
{
//****Set the layout
setLayout(new FlowLayout());
//****Initilize components
//Loading
m_BtnLoadInventory = new Button(" Load Inventory ");
m_TxtLoadInventory = new TextField("C:\\Users\\User\\Documents\\InputInventory.txt", 90);
m_BtnLoadItems = new Button("Load MarkedItems");
m_TxtLoadItems = new TextField("C:\\Users\\User\\Documents\\ItemsMarked.txt", 90);
// Info
m_TxtDescription = new TextField("Description", 110);
m_TxtItemNumber = new TextField("0.0000", 20);
m_TxtRetail = new TextField("$0.00", 20);
m_TxtDiscount = new TextField("00%", 20);
m_TxtPrice = new TextField("$0.00", 20);
m_TxtaOut = new TextArea("<HTML>", 15, 100);
//Next
m_BtnNext = new Button("Next");
//****Add components
//Loading
add(m_BtnLoadInventory);
add(m_TxtLoadInventory);
add(m_BtnLoadItems);
add(m_TxtLoadItems);
//Info
add(m_TxtDescription);
add(m_TxtItemNumber);
add(m_TxtRetail);
add(m_TxtDiscount);
add(m_TxtPrice);
add(m_TxtaOut);
//Next
add(m_BtnNext);
//Start Action listener
m_BtnLoadInventory.addActionListener(this);
m_BtnLoadItems.addActionListener(this);
m_BtnNext.addActionListener(this);
//Set Size
this.setSize(800, 600);
// Other
rt = Runtime.getRuntime();
}
@Override
public void actionPerformed(ActionEvent evt)
{
// Load Button, try to load the inventory
if (evt.getSource() == m_BtnLoadInventory)
{
if(m_TxtLoadInventory.getText().length() != 0)
{
m_sInventoryPath = m_TxtLoadInventory.getText();
m_sMarkedItemsPath = m_TxtLoadItems.getText();
System.out.println(m_sInventoryPath);
System.out.println(m_sMarkedItemsPath);
if( this.BuildInventory() )
m_TxtLoadInventory.setText(" Loaded Succesfully ");
else
{
m_TxtLoadInventory.setText(" FAILED ");
//System.exit(1);
}
if( this.BuildMarkedItems() )
{
m_TxtLoadItems.setText(" Loaded Succesfully ");
}
else
{
m_TxtLoadItems.setText(" FAILED ");
//System.exit(1);
}
}
}
else
// Next
if(evt.getSource() == m_BtnNext)
{
if(true)
{
// Find the index of the next item in the marked list
int index = findTerm(m_saMarkedItems[m_iNext]);
System.out.println(index);
// Increase next
m_iNext = m_iNext + 1;
// If not in the list
if (index < 0)
{
m_TxtDescription.setText("NOT FOUND IN INVENTORY");
m_TxtItemNumber.setText("!!!!!");
m_TxtRetail.setText("!!!!!");
m_TxtDiscount.setText("!!!!!");
m_TxtPrice.setText("!!!!!");
m_TxtaOut.setText("!!!!!");
}
else
{
if(m_saNumber[index].equals(""))
{
m_TxtaOut.setText("------- No Number --------");
m_TxtRetail.setText(m_saRetail[index]);
m_TxtPrice.setText(m_saPrice[index]);
m_TxtDescription.setText(m_saDescription[index]);
}
else
{
//Update txtOut
m_TxtItemNumber.setText(m_saInventory[index]);
m_TxtDiscount.setText(m_saDiscount[index]);
m_TxtRetail.setText(m_saRetail[index]);
m_TxtPrice.setText(m_saPrice[index]);
m_TxtDescription.setText(m_saDescription[index]);
}
}
}
}
}
boolean BuildInventory()
{
Scanner in = null;
try
{
in = new Scanner(new File(m_sInventoryPath));
}
catch (FileNotFoundException e)
{
System.out.println("Cannot open inventory");
return false;
}
int i = 0;
while (in.hasNextLine())
{
int j = 0;
StringTokenizer tkner = new StringTokenizer(in.nextLine());
while (tkner.hasMoreTokens())
{
if(j == 0)
m_saInventory[i] = tkner.nextToken("|");
else if(j == 1)
m_saNumber[i] = tkner.nextToken("|");
else if(j == 2)
m_saDescription[i] = tkner.nextToken("|");
else if(j == 3)
m_saRetail[i] = tkner.nextToken("|");
else if(j == 4)
m_saDiscount[i] = tkner.nextToken("|");
else if(j == 5)
m_saPrice[i] = tkner.nextToken("|");
else
tkner.nextToken("|");
j++;
}
i++;
}
in.close();
return true;
}
boolean BuildMarkedItems()
{
Scanner in = null;
try
{
File f = new File(m_sMarkedItemsPath);
System.out.println(f.getPath());
in = new Scanner(f);
}
catch (FileNotFoundException e)
{
e.printStackTrace(System.out);
System.out.println("Cannot open Marked Items");
return false;
}
int i = 0;
while (in.hasNextLine())
{
int j = 0;
StringTokenizer tkner = new StringTokenizer(in.nextLine());
while (tkner.hasMoreTokens())
{
if(j == 0)
m_saMarkedItems[i] = tkner.nextToken("|");
if(j == 1)
m_saActive[i] = tkner.nextToken("|");
else
tkner.nextToken("|");
j++;
}
i++;
}
in.close();
return true;
}
int findTerm(String key)
{
int lo = 0; int hi = m_iInventorySize - 1;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
int diff = key.compareTo(m_saInventory[mid]);
if (diff == 0) return mid;
if (diff < 0) hi = mid - 1; else lo = mid + 1;
}
return -1;
}
}
堆栈跟踪:::::
C:\Users\User\Documents\InputInventory.txt
C:\Users\User\Documents\ItemsMarked.txt
Cannot open inventory
C:\Users\User\Documents\ItemsMarked.txt
java.io.FileNotFoundException: C:\Users\User\Documents\ItemsMarked.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at CLInventorizerMaster.BuildMarkedItems(CLInventorizerMaster.java:253)
at CLInventorizerMaster.actionPerformed(CLInventorizerMaster.java:131)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Cannot open Marked Items