Java代码无法找到或定位指定的文件路径
所以,我可能遗漏了一些非常明显的东西,但据我所知,我已经尝试了所有的方法。基本上我有一个删除代码,它根据关键字从txt文件中提取路径。每次我尝试它似乎都找不到文件路径。因此,我尝试将从文件中提取路径的代码替换为代码中的直接路径,但仍然不起作用。这是我的问题,这是我的代码。任何帮助都会有帮助Java代码无法找到或定位指定的文件路径,java,file,path,Java,File,Path,所以,我可能遗漏了一些非常明显的东西,但据我所知,我已经尝试了所有的方法。基本上我有一个删除代码,它根据关键字从txt文件中提取路径。每次我尝试它似乎都找不到文件路径。因此,我尝试将从文件中提取路径的代码替换为代码中的直接路径,但仍然不起作用。这是我的问题,这是我的代码。任何帮助都会有帮助 import java.util.*; import java.io.*; import java.nio.*; public class test { public static void main (
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.");
}
}
}
我的command.txt包含以下行
delete:delete
remove:delete
d:delete
再次感谢您的帮助。我建议您在尝试删除该文件之前,检查该文件的路径是否存在。如下
File f1 = new File(path);
boolean sucess;
if (f1.exists()) {
success = f1.delete();
} else {
System.out.println(
"I cannot find '" + f1.getAbsolutePath() );
}
如果找不到文件路径,这将打印文件路径
或者,您可以使用在哪一行获得异常?没有异常行,结果显示删除失败,就像第243行中所说的那样,您发布了大量与项目无关的代码。如果你想完全理解你的问题,如果你想让我们完全理解你的问题,你需要创建一个新的程序,它什么也不做,只是把问题归结为最基本的问题。也许它所做的只是创建一个文件,然后删除它,但不管怎样,这个程序应该重新创建您的错误,而不做其他任何事情,一个。好吧,问题是它本身工作完美,但一旦我将其插入完整的代码中,它就会中断…捕获其中的所有异常。使用异常而不是指定它。。!!你可能会得到一个例外。!!
File f1 = new File(path);
boolean sucess;
if (f1.exists()) {
success = f1.delete();
} else {
System.out.println(
"I cannot find '" + f1.getAbsolutePath() );
}