Is it possible to write to an existing text file in a jar. I managed to read from it with getResource() but I am stuck trying to write to it, it gives me a file not found exception.
Thanks
Is it possible to write to an existing text file in a jar. I managed to read from it with getResource() but I am stuck trying to write to it, it gives me a file not found exception.
Thanks
yes, that is possible ...
might be easier to spot your mistakes if you post your code.
This class is part of a game I'm making. It is used for I/O of the game records
I am having problem with the method saveRecords() after creating the runnable jar with eclipse, is there another way to write to this file?
package Data;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;
public class Records
{
public enum Difficulty
{
BEGINNER, INTERMEDIATE, EXPERT
};
private Scanner in;
private PrintWriter out;
File f;
private Vector<String> b, i, e;
public Records()
{
b = new Vector<String>();
i = new Vector<String>();
e = new Vector<String>();
URL url;
try
{
url = this.getClass().getResource("beginner.txt");
f = new File(url.getFile());
} catch (Exception e1)
{
System.out.println("File not found");
}
try
{
in = new Scanner(this.getClass().getResourceAsStream("beginner.txt"));
}
catch(NullPointerException e)
{
System.out.println("Stream not foound");
}
get();
}
private void get()
{
Difficulty current = null;
while (in.hasNextLine())
{
String n = in.nextLine();
if (n == "")
continue;
if (n.equals("b"))
{
current = Difficulty.BEGINNER;
continue;
}
else if (n.equals("i"))
{
current = Difficulty.INTERMEDIATE;
continue;
}
else if (n.equals("e"))
{
current = Difficulty.EXPERT;
continue;
}
if (current == Difficulty.BEGINNER)
{
b.add(n);
}
else if (current == Difficulty.INTERMEDIATE)
i.add(n);
else if (current == Difficulty.EXPERT)
e.add(n);
}
sortRecords();
}
public void addRecord(Difficulty current, int n)
{
if (current == Difficulty.BEGINNER)
b.add("" + n);
else if (current == Difficulty.INTERMEDIATE)
i.add("" + n);
else if (current == Difficulty.EXPERT)
e.add("" + n);
sortRecords();
}
public Vector<String> getRecords(Difficulty d)
{
Vector<String> c = null;
if (d == Difficulty.BEGINNER)
c = b;
else if (d == Difficulty.INTERMEDIATE)
c = i;
else if (d == Difficulty.EXPERT)
c = e;
return c;
}
public void saveRecords()
{
try
{
out = new PrintWriter(f) ;
} catch (FileNotFoundException e1)
{
System.out.println("file not found");
e1.printStackTrace();
}
out.println("b");
for (int i = 0; i < b.size(); i++)
out.println(b.get(i));
out.println("i");
for (int j = 0; j < i.size(); j++)
out.println(i.get(j));
out.println("e");
for (int i = 0; i < e.size(); i++)
out.println(e.get(i));
out.flush();
out.close();
}
private void sortRecords()
{
Collections.sort(b);
Collections.sort(i);
Collections.sort(e);
}
public void removeRecords(Difficulty d, int numberOfRecords)
{
Vector<String> c = null;
if (d == Difficulty.BEGINNER)
c = b;
else if (d == Difficulty.INTERMEDIATE)
c = i;
else if (d == Difficulty.EXPERT)
c = e;
for (int i = 0; i < numberOfRecords; i++)
c.remove(c.size() - 1);
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.