简体   繁体   English

Java代码无法找到或找到指定的文件路径

[英]Java code can't find or locate specified file path

So, I may be missing something extremely obvious here but as far as I can see I've tried everything. 因此,在这里我可能会遗漏一些非常明显的东西,但据我所知,我已经尝试了一切。 Basically what I have is a deletion code which pulls the path from a txt file based on a keyword. 基本上,我有一个删除代码,该代码根据关键字从txt文件中提取路径。 Every time I try it can't seem to find the file path. 每次尝试时,似乎都找不到文件路径。 So I tried replacing the code that pulled the path from the file with a direct path in the code and it still doesn't work. 因此,我尝试用代码中的直接路径替换从文件中拉出路径的代码,但仍然无法正常工作。 So those are my troubles and here's my code. 这些就是我的麻烦,这是我的代码。 Any help would be helpful. 任何帮助都会有所帮助。

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

public class test {

public static void main (String[] args)
{
    test t = new test();
    t.test();
}

/** A test method for our code **/
public void test()
{
    String[] command = new String[3];
    String[] path = new String[3];

    BufferedReader commands = null;
    BufferedReader paths = null;

    //initialize list of commands
    ArrayList<String[]> commandList = new ArrayList<String[]>();
    ArrayList<String[]> pathList = new ArrayList<String[]>();

    //Get a list of Commands from a file
    this.getCommands( commandList );
    this.getPaths( pathList );

    //get the next command
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Next Thing?\n");
    this.getCommand( br, command, commandList );
    if ( command[1] == null )
    {
        System.out.print("New command?\n");
        commandList.add( this.makeCommand( br, command ) );
    }
    else
    {
        //Not sure what you want to do here, this is if the method IS found
        if (command[1].equals("delete"))
        {
            BufferedReader brp = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Which file?\n");
            this.getPath( brp, path, pathList );
            if ( path[1] == null )
            {
                System.out.print("New command?\n");
                pathList.add( this.makePath( brp, path ) );
            }
            else
            {
                System.out.print(path[1]+":" +path[2]+" ");
                deletefile(path[1]+":" +path[2]);
            }
            this.savePath(pathList); 
        }
        else
        {
            System.out.print("Derps");
        }
        System.out.println( "We found: "+command[1]);
    }
    this.save(commandList);  

}

/**Returns the old list of commands**/
public void getCommands( ArrayList<String[]> commandList )
{
    BufferedReader commands;
    try
    {
        String sCurrentLine;

    commands = new BufferedReader(new FileReader("commands.txt"));

    while ((sCurrentLine = commands.readLine()) != null)
        {
            commandList.add( sCurrentLine.split(":") );
        }

    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

/**
Asks the user for a command and checks it against known commands. It's not a 
very efficient algorithm, but effective. 
**/
public void getCommand( BufferedReader br, String[] command, ArrayList<String[]> commandList )
{
    try 
    {
        command[0] = br.readLine();
        for ( String[] com : commandList )
        {
            if (com[0].equals( command[0] ))
            {
                command[1] = com[1];
            }
        }
    }
    catch (IOException ioe) 
    {
        System.out.println("IO error trying to read your commnad!");
    }
}

/** Makes a new command, to be used when one isn't known **/
public String[] makeCommand( BufferedReader br, String[] command )
{
    try
    {
        command[1] = br.readLine();
    }
    catch( IOException ioe)
    {
        System.out.println("Oh no!!!");
    }
    return command;
}

/** Saves your stuff **/
public void save( ArrayList<String[]> commandList){
    try
    {
        PrintWriter writer = new PrintWriter( "commands.txt","UTF-8" );
        for ( String[] com : commandList )
        {
            writer.println( com[0]+":"+com[1] );
        }
        writer.close();
    }
    catch( Exception ioe )
    {
        System.out.println("You're in trouble");
    }
}






/**Returns the old list of commands**/
public void getPaths( ArrayList<String[]> pathList )
{
    BufferedReader paths;
    try
    {
        String sCurrentLine;

    paths = new BufferedReader(new FileReader("paths.txt"));

    while ((sCurrentLine = paths.readLine()) != null)
        {
            pathList.add( sCurrentLine.split(":") );
        }

    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

/**
Asks the user for a path and checks it against known paths. It's not a 
very efficient algorithm, but effective. 
**/
public void getPath( BufferedReader brp, String[] path, ArrayList<String[]> pathList )
{
    try 
    {
        path[0] = brp.readLine();
        for ( String[] pat : pathList )
        {
            if (pat[0].equals( path[0] ))
            {
                path[1] = pat[1];
                path[2] = pat[2];
            }
        }
    }
    catch (IOException ioe) 
    {
        System.out.println("IO error trying to read your commnad!");
    }
}

/** Makes a new path, to be used when one isn't known **/
public String[] makePath( BufferedReader brp, String[] path )
{
    try
    {
        path[1] = brp.readLine();
        System.out.println("location");
        path[2] = brp.readLine();
    }
    catch( IOException ioe)
    {
        System.out.println("Oh no!!!");
    }
    return path;
}

/** Saves your stuff **/
public void savePath( ArrayList<String[]> pathList){
    try
    {
        PrintWriter writerPath = new PrintWriter( "paths.txt","UTF-8" );
        for ( String[] pat : pathList )
        {
            writerPath.println( pat[0]+":"+pat[1]+":"+pat[2]);
        }
        writerPath.close();
    }
    catch( Exception ioe )
    {
        System.out.println("You're in trouble");
    }
}








/** Deletes files **/
private static void deletefile(String file)
{
    File f1 = new File(file);
    boolean success = f1.delete();
    if (!success)
    {
        System.out.println("Deletion failed.");
        System.exit(0);
    }
    else
    {
        System.out.println("File deleted.");
    }
}

}

The only line I have in my path.txt file is: test file 我的path.txt文件中仅有的一行是:测试文件

:C:/User/Carter/Desktop/AITest/test/test.txt

My command.txt has the lines 我的command.txt包含以下行

delete:delete
remove:delete
d:delete

Thanks again for any help. 再次感谢任何帮助。

I suggest you check the path of the file and that is exists before you try and delete it. 建议您在尝试删除文件之前检查该文件的路径。 Like below 像下面

File f1 = new File(path);
boolean sucess;
if (f1.exists()) {
    success = f1.delete();
} else {
    System.out.println(
        "I cannot find '" + f1.getAbsolutePath() );
}

This will print the file path if it cannot be found. 如果找不到,将打印文件路径。

Alternatively you could use Files.delete(Path) 或者,您可以使用Files.delete(Path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM