.getAbsolutePath函数在eclipse和cmd中的工作方式不同

问题描述

我有一个扫描文件的程序。

import java.util.*;
import java.io.*;

public class testProgram {

private static Scanner scan;
private static File file;
private static String filePath;
private static String content;

public static void main(String[] args) throws InterruptedException{
    
    filePath = "files\\text.txt";
    
    file = new File(filePath);
    file.getAbsoluteFile();
    
    try {
        
        scan = new Scanner(file);
        
        while(scan.hasNextLine()) {
            
            content = scan.nextLine();
            System.out.println(content);
            
        }
        
        Thread.sleep(10000);
        
    } 
    
    catch (FileNotFoundException e) {
        
        System.out.println("file not found");
        Thread.sleep(10000);

    }

}

}

我试图在Eclipse中运行它,并且效果很好。绝对路径显示如下:

“ D:\ Programming \ Java \ Test Program \ files \ text.txt”

当我将其编译为批处理文件并在cmd中运行该程序时,该程序无法运行,因为现在的绝对路径不同:

“ D:\ Programming \ Java \ Test Program \ src \ files \ text.txt”

这是我的批处理文件的样子:

回音

cls

java testProgram

尽管java文件(在Eclipse中工作的那个)和批处理文件(不工作的)位于同一文件夹中,但是批处理文件文件路径方面的行为有所不同。我希望他们在日食或cmd中工作。为什么会发生这种情况,有可能解决吗?对不起,麻烦了,初学者在这里:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)