我必须为学校制作一个鱼雷游戏,并为其提供顶级清单。我想将它存储在JAR附近的文件夹结构中:/Torpedo/local/toplist/top_i.dat,其中i是该分数的位置。这个调用将在程序的第一次启动时创建文件:
File f;
f = new File(Toplist.toplistPath+"/top_1.dat");
if(!f.exists()){
Toplist.makeToplist();
}
这是toplist类:
package main;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class Toplist {
static String toplistPath = "./Torpedo/local/toplist"; //I know it won't work this easily, it's only to get you the idea
public static JFrame toplistWindow = new JFrame("Torpedó - [TOPLISTA]");
public static JTextArea toplist = new JTextArea("");
static StringBuffer toplistData = new StringBuffer(3000);
public Toplist() {
toplistWindow.setSize(500, 400);
toplistWindow.setLocationRelativeTo(null);
toplistWindow.setResizable(false);
getToplist();
toplist.setSize(400, 400);
toplist.setLocation(0, 100);
toplist.setColumns(5);
toplist.setText(toplistData.toString());
toplist.setEditable(false);
toplist.setBackground(Color.WHITE);
toplistWindow.setLayout(null);
toplistWindow.setVisible(true);
}
public Toplist(Player winner) {
//this is to be done yet, this will set the toplist at first and then display it
toplistWindow.setLayout(null);
toplistWindow.setVisible(true);
}
/**
* Creates a new toplist
*/
public static void makeToplist(){
new File(toplistPath).mkdir();
for(int i = 1; i <= 10; i++){
File f = new File(toplistPath+"/top_"+i+".dat");
try {
f.createNewFile();
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "Fájl hiba: toplista létrehozása", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* If the score is a top score it inserts it into the list
*
* @param score - the score to be checked
*/
public static void setToplist(int score, Player winner){
BufferedReader input = null;
PrintWriter output = null;
int topscore;
for(int i = 1; i <= 10; i++){
try {
input = new BufferedReader(new FileReader(toplistPath+"/top_"+i+",dat"));
String s;
topscore = Integer.parseInt(input.readLine());
if(score > topscore){
for(int j = 9; j >= i; j--){
input = new BufferedReader(new FileReader(toplistPath+"/top_"+j+".dat"));
output = new PrintWriter(new FileWriter(toplistPath+"/top_"+(j+1)+".dat"));
while ((s = input.readLine()) != null) {
output.println(s);
}
}
output = new PrintWriter(new FileWriter(toplistPath+"/top_"+i+".dat"));
output.println(score);
output.println(winner.name);
if(winner.isLocal){
output.println(Torpedo.session.remote.name);
}else{
output.println(Torpedo.session.remote.name);
}
output.println(Torpedo.session.mapName);
output.println(DateUtils.now());
break;
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(new JFrame(), "Fájl hiba: toplista frissítése", "Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "Fájl hiba: toplista frissítése", "Error", JOptionPane.ERROR_MESSAGE);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "Fájl hiba: toplista frissítése", "Error", JOptionPane.ERROR_MESSAGE);
}
}
if (output != null) {
output.close();
}
}
}
}
/**
* This loads the toplist into the buffer
*/
public static void getToplist(){
BufferedReader input = null;
toplistData = null;
String s;
for(int i = 1; i <= 10; i++){
try {
input = new BufferedReader(new FileReader(toplistPath+"/top_"+i+".dat"));
while((s = input.readLine()) != null){
toplistData.append(s);
toplistData.append('\t');
}
toplistData.append('\n');
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(new JFrame(), "Fájl hiba: toplista betöltése", "Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "Fájl hiba: toplista betöltése", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
/**
*
* @author http://www.rgagnon.com/javadetails/java-0106.html
*
*/
public static class DateUtils {
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
}
}
问题是,它无法访问任何文件。我已经尝试将它们添加到类路径中,并且我在网上找到了至少六种不同的文件/路径处理变量,但没有任何效果。 谁能告诉我,我做错了什么?
谢谢。
答案 0 :(得分:1)
处理文件时,路径与执行应用程序的位置有关。从eclipse运行时,基本路径是项目文件夹。
如果文件不存在,您通常会在父项上调用mkdirs()
来创建文件夹层次结构,然后创建文件。