我有一个学校项目,它将从我的笔记本电脑转移到教师的电脑上,供老师评估。问题是我的代码一直说没有找到文件“songlist.txt”。我正在将songlist.txt和代码转移到教师的计算机上。我确保我正确拼写了拼写文件的名称,并使用了一种名为
的方法 System.out.println(System.getproperty("user.dir"));
。
输出没有显示代码所在的文件夹。
输出结果为:
E:\ Projects \ Java \ work应该是
E:\项目\爪哇\工作\ MP3
我正在使用Ready To Program作为编辑器和编译器btw。
mp3_organizer.java
public class mp3_organizer
{
static BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
static final int max = 50;
static final String file = "E:\\Projects\\Java\\ICS3U Work\\src\\mp3\\songlist.txt";
public static void newsong (String file) throws IOException
{
BufferedWriter writer = new BufferedWriter (new FileWriter (file, true));
System.out.println ("[ADDING songS] \n");
System.out.println ("Enter the name of the song");
String name = stdin.readLine ();
System.out.println ("Enter a file address of song");
String filePath = stdin.readLine ();
System.out.println ("Enter the artist's name or put N/A if unknown");
String songArtist = stdin.readLine ();
System.out.println ("Enter the genre");
String songGenre = stdin.readLine ();
writer.write ("");
writer.newLine ();
writer.write (name);
writer.newLine ();
writer.write (filePath);
writer.newLine ();
writer.write (songArtist);
writer.newLine ();
writer.write (songGenre);
writer.newLine ();
writer.close ();
}
static void search (String[] songname, String[] songpath, String[] artist, String[] genre, int count) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String search;
boolean find = false;
int select;
System.out.println ("Please select what category you wish to search by:\n(1) song\n(2) Artist\n(3) Genre\n(4) Song Path\n(0) Cancel");
do
{
select = Integer.parseInt (stdin.readLine ());
switch (select)
{
case 1:
System.out.print ("Please enter the name of the song to search for: ");
search = stdin.readLine ();
for (int i = 0 ; i < count ; i++)
{
if (songname [i].equalsIgnoreCase (search))
{
find = true;
}
}
if (find)
{
System.out.println ("song\tsong\tArtist\tGenre");
for (int i = 0 ; i < count ; i++)
{
if (songname [i].equalsIgnoreCase (search))
{
System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
}
}
}
else
{
System.out.println (search + " was not found in your library.");
}
break;
case 2:
System.out.print ("Please enter the name of the artist to search for: ");
search = stdin.readLine ();
for (int i = 0 ; i < count ; i++)
{
if (artist [i].equalsIgnoreCase (search))
{
find = true;
}
}
if (find)
{
System.out.println ("song\tsongpath\tArtist\tGenre");
for (int i = 0 ; i < count ; i++)
{
if (artist [i].equalsIgnoreCase (search))
{
System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
}
}
}
else
{
System.out.println ("songs by " + search + " were not found in any locations.");
}
break;
case 3:
System.out.print ("Please enter the genre to search for: ");
search = stdin.readLine ();
for (int i = 0 ; i < count ; i++)
{
if (genre [i].equalsIgnoreCase (search))
{
find = true;
}
}
if (find)
{
System.out.println ("song\tsongpath\tArtist\tGenre");
for (int i = 0 ; i < count ; i++)
{
if (genre [i].equalsIgnoreCase (search))
{
System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
}
}
}
else
{
System.out.println (search + " was not found in any locations.");
}
break;
case 4:
System.out.print ("Please enter the file address of the song to search for: ");
search = stdin.readLine ();
for (int i = 0 ; i < count ; i++)
{
if (songname [i].equalsIgnoreCase (search))
{
find = true;
}
}
if (find)
{
System.out.println ("song\tsongpath\tArtist\tGenre");
for (int i = 0 ; i < count ; i++)
{
if (songname [i].equalsIgnoreCase (search))
{
System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
}
}
}
else
{
System.out.println (search + " was not found in your library.");
}
break;
case 0:
break;
default:
System.out.println ("Please enter a valid option: ");
}
}
while (select > 3);
System.out.println ();
}
public static void backup (String file, int count, String[] songname, String[] songpath, String[] artist, String[] genre) throws IOException
{
BufferedWriter writer = new BufferedWriter (new FileWriter ("backup.txt"));
for (int i = 0 ; i < count ; i++)
{
writer.write (songname [i]);
writer.newLine ();
writer.write (songpath [i]);
writer.newLine ();
writer.write (artist [i]);
writer.newLine ();
writer.write (genre [i]);
writer.newLine ();
}
writer.close ();
}
public static void printlist (int count, String[] songname, String[] songpath, String[] artist, String[] genre)
{
for (int i = 0 ; i < count ; i++)
{
System.out.println (songname [i] + "\n" + songpath [i] + "\n" + artist [i] + "\n" + genre [i]);
}
}
public static void test ()
{
File songlist = new File ("songlist.txt");
System.out.println (System.getProperty ("user.dir"));
if (songlist.exists ())
{
if (songlist.canRead ())
{
System.out.print ("file can be read");
}
else
{
System.out.print ("File cannot be read");
}
}
else
{
System.out.println ("file does not exist");
}
}
public static void main (String str[]) throws FileNotFoundException, IOException
{
test();
int count = 0;
String[] songname = new String [max];
String[] songpath = new String [max];
String[] artist = new String [max];
String[] genre = new String [max];
BufferedReader reader = new BufferedReader (new FileReader ("songlist.txt"));
System.out.println ("[OPTIONS] \n" +
"1) Print the song list \n" +
"2) Add a song to list \n" +
"3) Search for a song \n" +
"4) Sort the song list \n" +
"5) Create a backup of the song lsit \n" +
"6) Delete a song from the collection \n" +
"7) Create a new profile");
System.out.println ("Enter a option");
int choice = Integer.parseInt (stdin.readLine ());
String temp, line = null;
while ((line = reader.readLine ()) != null)
{
songname [count] = line;
songpath [count] = reader.readLine ();
artist [count] = reader.readLine ();
genre [count] = reader.readLine ();
count++;
}
reader.close ();
switch (choice)
{
case 1:
System.out.println ("[PRINTING song LIST] \n");
printlist (count, songname, songpath, artist, genre);
break;
case 2:
newsong (file);
break;
case 3:
System.out.println ("[SONG SEARCH]");
search (songname, songpath, artist, genre, count);
break;
case 4:
System.out.println ("[SONGLIST SORT]");
break;
case 5:
System.out.println ("[CREATING BACKUP OF SONGLIST");
backup (file, count, songname, songpath, artist, genre);
break;
case 6:
System.out.println ("[REMOVE SONG FROM LIST]");
break;
case 7:
System.out.println ("[CREATE NEW PROFILE]");
break;
}
}
}