所以,我可能在这里遗漏了一些非常明显的东西,但据我所知,我已经尝试了一切。基本上我所拥有的是一个删除代码,它根据关键字从 txt 文件中提取路径。每次我尝试它似乎都找不到文件路径。所以我尝试用代码中的直接路径替换从文件中提取路径的代码,它仍然不起作用。所以这些是我的麻烦,这是我的代码。任何帮助都会有所帮助。
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.");
}
}
}
我在 path.txt 文件中的唯一行是:测试文件
:C:/User/Carter/Desktop/AITest/test/test.txt
我的 command.txt 有这些行
delete:delete
remove:delete
d:delete
再次感谢任何帮助。