2013-01-09 3 views
0

Je reçois une erreur quand il essaie de créer le scanner pour les fichiers dans les fonctions de construction. Une fois le fichier créé, il possède le chemin d'accès au fichier, mais lorsque l'analyseur tente d'utiliser le fichier, il ne peut pas trouver le fichier en question. Il échoue pour les deux scanners. Je viens de commencer à travailler dessus il y a un petit moment, et avant cela, ça marchait bien, je n'ai pas modifié les méthodes de construction ou la section de balayage pour déboguer, donc je sais que c'est toujours la même chose. (La première méthode de construction a enlevé le code de débogagez). *** Je posterai une image si les gens veulent du fichier, assis là dans "C: \ Users \ User \ Documents \", je sais qu'on me demandera si le fichier est là.Java, Impossible de trouver le fichier texte, avec un StkTrc

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; 
} 

} 

STACK TRACE :::::

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 
+0

pouvez-vous essayer avec des barres obliques '/' au lieu de barres obliques inversées? (par exemple: C: /Users/User/Documents/ItemsMarked.txt) –

+0

Échec de Majid –

Répondre

0

La trace de pile est indiquant que l'applet n'a pas accès au fichier qui provoque SecurityManager de l'applet pour produire un FileNotFoundException plus haut dans la pile.

Les applets sont mises en sandbox pour empêcher les attaques malveillantes et doivent être signées pour accéder aux ressources locales.


Au lieu de AWT, d'envisager un moyen plus à jour de représenter votre client tel que Java Swing et déployer également une application basée sur JFrameJava Web Start.

Questions connexes