I have a question about package structure and reading and writing into files.
I am coding an eshop to learn java and now implented data exchange via sockets.
Since I am not using a data bank I am using .txt-files to write articles, new customers etc. into it.
I now wanted to place the txt files into the server module in a package named "ressources". But then the compiler always said he couldn't find the files. I then named him the exact path to the "ressources" package and the reading worked.
public void openForReading(String dataSource) throws FileNotFoundException {
//New code
InputStream is = this.getClass().getResourceAsStream("/" + dataSource);
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
//old code, compiler said he couldn't find the file
//reader = new BufferedReader(new FileReader(dataSource));
}
But when I registered a new customer and wrote him into the files he created a new file in the project directory instead of refreshing the file on the server in the package "ressources". I could fix that too with this code. but now he only refreshes the files in the directory "out".
public void openForWriting(String dataSource) throws IOException {
//New code
URL url = this.getClass().getResource("/" + dataSource);
File file = new File(url.getPath());
writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
//old code, not working - creates new file
//writer = new PrintWriter(new BufferedWriter(new FileWriter(dataSource)));
}
Is there a way to refresh the files on the server and in the directory out?
${user.home}
mkdir "%USERPROFILE%\.EShop\data"
from cmd.exe. Put your data files in thereexplorer "%USERPROFILE%\.EShop\data"