未找到Java文件异常

时间:2015-12-11 03:22:08

标签: java file exception

我正在使用sidescroller而我的BlockManager Class出现问题。问题是在名为“Images”的文件夹中读取我的文本文件。我在该文件夹中的图像加载正常,所以我不知道发生了什么。

每当我运行我的代码时,我得到“java.io.FileNotFoundException:Images \ blocksInfo.txt”。我想要读取的文件的目录是/Images/blocksInfo.txt。

我真的很困惑为什么我正在搜索的目录使用正斜杠而eclipse在显示异常时将它们显示为反斜杠。我认为它可能与我用来搜索文件的字符串“Images /”有关,但我不确定

这是代码,问题出在loadBlocksFile方法中:

  public class BlockManager {

  private final static String IMAGE_DIR = "Images/";
  private final static int MAX_BLOCKS_LINES = 15;

  private final static double MOVE_FACTOR = 0.25;  

  private int pWidth, pHeight;    
  private int width, height;     

  private int imWidth, imHeight;  
  private int numCols, numRows;  

  private int xMapHead;    

  @SuppressWarnings("rawtypes")
private ArrayList blocksList;   
    // stores Brick objects which makes up the block map

  @SuppressWarnings("rawtypes")
private ArrayList[] columnBlocks;
    // Brick objects saved in column order 
    // (faster to search than blocksList)

  private ImageLoader imsLoader;
  @SuppressWarnings("rawtypes")
private ArrayList blockImages = null;    
         // holds all the images loaded by imsLoader


  @SuppressWarnings("rawtypes")
public BlockManager(int w, int h, String fnm, ImageLoader il)
  {
    pWidth = w; pHeight = h;
    imsLoader = il;

    blocksList = new ArrayList();
    loadBlocksFile(fnm);
    initBlocksInfo();
    createColumns();

    moveSize = (int)(imWidth * MOVE_FACTOR);
    if (moveSize == 0) {
      System.out.println("moveSize cannot be 0, setting it to 1");
      moveSize = 1;
    }

    isMovingRight = false;   // no movement at start
    isMovingLeft = false;
    xMapHead = 0;
  }  

  private void loadBlocksFile(String fnm){ 
    String imsFNm = IMAGE_DIR + fnm;
    System.out.println("Reading blocks file: " + imsFNm);

    int numStripImages = -1;
    int numBlocksLines = 0;
    try {
      BufferedReader br = new BufferedReader( new FileReader(imsFNm));
      String line;
      char ch;
      while((line = br.readLine()) != null) {
        if (line.length() == 0)  
          continue;
        if (line.startsWith("//"))   
          continue;
        ch = Character.toLowerCase( line.charAt(0) );
        if (ch == 's')  
          numStripImages = getStripImages(line);
        else {  
          if (numBlocksLines > MAX_BLOCKS_LINES) 
            System.out.println("Max reached, skipping blocks line: " + line);
          else if (numStripImages == -1) 
            System.out.println("No strip image, skipping blocks line: " + line);
          else {
            storeBlocks(line, numBlocksLines, numStripImages);
            numBlocksLines++;
          }
        }
      }
      br.close();
    } 
    catch (IOException e) 
    { System.out.println(e);
      System.exit(1);
    }
  } 

2 个答案:

答案 0 :(得分:0)

  

每当我运行我的代码时,我都会得到“java.io.FileNotFoundException:Images \ blocksInfo.txt”。

因此,您提供的文件名为"Images\\blocksInfo.txt"

  

我想要阅读的文件的目录是/Images/blocksInfo.txt。

所以你省略了一个前导斜杠。

答案 1 :(得分:0)

我发现了问题。它不是目录,我使用的是“BufferedReader br = new BufferedReader(new FileReader(”imsFNm“));”而不是“BufferedReader br = new BufferedReader(new InputStreamReader(in));”