微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决.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 举报,一经查实,本站将立刻删除。