Java Create File and Directory if needed - java

I want to create a file (outside of the workspace) so that everyone who opens my program has his own Textfile.
Currently I have to following Code:
private static final File m_dataFile = new File("C:\\temp\\MainPlayersLoginData.txt");
private static FileWriter writer;
private static Scanner reader;
public static void setMainPlayersLoginData(String name, String password) {
try {
if (!m_dataFile.exists()) {
createDirectory();
}
writer = new FileWriter(m_dataFile);
writer.write(name + "\n" + password);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void createDirectory() {
System.out.println("creating directory: " + m_dataFile.getName());
boolean result = false;
try {
m_dataFile.mkdirs();
result = true;
} catch (SecurityException se) {
}
if (result) {
System.out.println("DIR created");
}
}
With this code, the program creates a folder temp as planned, but creates a folder named "MainPlayersLoginData.txt" in it instead of a textfile. In addition I get a FileNotFoundException with the message "Access denied" when initialising the FileWriter.
I tried using m_datafile.mkdir() instead of m_datafile.mkdirs() but this time I get a FileNotFoundException with the message "The system cannot find the specified path" and the folder temp isnt created.
Edit: If i create the folder and the Textfile on my own, everything works fine.

Related

How to prevent the block from being displayed, finally, if there is no path to the file

Problem: if the path to the file was not specified in the arguments, then it still displays the phrase "The file was closed". This works 2 times. In uploadToFile and read method. I pass one path in the arguments, and the second is written in the DownloadFile
public class Task implements AutoCloseable {
public static void main(String[] args) throws IOException {
String DownloadFile = "C:\\Users\\VGilenko\\IdeaProjects\\Task\\src\\main\\resources\\Out.txt";
Map<String, Departament> departments = new HashMap<>();
String path = args.length > 0 ? args[0] : null;
read(path, departments);
transferToDepartment(departments, DownloadFile);
}
private static void uploadToFile(List download, String path) {
int i = 0;
try (FileWriter writer = new FileWriter(path, false)) {
...
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
System.out.println("The file was closed");
}
}
public static void transferToDepartment(Map<String, Departament> departments, String downloadFile) {
List<String> download = new ArrayList<>();
...
}
uploadToFile(download, downloadFile);
}
public static void read(String path, Map<String, Departament> departments) throws IOException {
assert path != null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "CP1251")); br) {
.....
}
}
} catch (FileNotFoundException e) {
System.out.println("The file was not found, check the path");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Correct the file path, step out of the array");
} catch (NullPointerException e) {
System.out.println("You forgot to register the path to the file");
} finally {
System.out.println("The file was closed");
}
}
#Override
public void close() {
System.out.println("The file was closed");
}
}
You have your printout "The file was closed" in your finally statement. If you don't specify a file, you will catch an Exception, and your finally block will be executed.
An easy fix would be to check for the existence of the path (not being empty, not being null).

Csv file is empty when I writing content

I am trying write to a csv file. After the execution of the code bellow the csv file is still empty.
File is in folder .../webapp/resources/.
This is my dao class:
public class UserDaoImpl implements UserDao {
private Resource cvsFile;
public void setCvsFile(Resource cvsFile) {
this.cvsFile = cvsFile;
}
#Override
public void createUser(User user) {
String userPropertiesAsString = user.getId() + "," + user.getName()
+ "," + user.getSurname() +"\n";;
System.out.println(cvsFile.getFilename());
FileWriter outputStream = null;
try {
outputStream = new FileWriter(cvsFile.getFile());
outputStream.append(userPropertiesAsString);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public List<User> getAll() {
return null;
}
}
This is a part of beans.xml.
<bean id="userDao" class="pl.project.dao.UserDaoImpl"
p:cvsFile="/resources/users.cvs"/>
Program compiles and doesn't throw any exceptions but CSV file is empty.
If you're running your app in IDE, the /webapp/resources used for running app will differ from the /webapp/resources in your IDE. Try to log full path to file and check there.
try using outputStream.flush() as the final statement in the first of the try block.
I think you're looking at the wrong file. If you specify an absolute path /resources/users.cvs, then it probably won't be written into the a folder relative to the webapp. Instead, it will be written to /resources/users.cvs
So the first step is to always log an absolute path to make sure the file is where you expect it.
Try with this code, it will at least tell you where the problem lies (Java 7+):
// Why doesn't this method throw an IOException?
#Override
public void createUser(final User user)
{
final String s = String.format("%s,%s,%s",
Objects.requireNonNull(user).getId(),
user.getName(), user.getSurname()
);
// Note: supposes that .getFile() returns a File object
final Path path = csvFile.getFile().toPath().toAbsolutePath();
final Path csv;
// Note: this supposes that the CSV is supposed to exist!
try {
csv = path.toRealPath();
} catch (IOException e) {
throw new RuntimeException("cannot locate CSV " + path, e);
}
try (
// Note: default is to TRUNCATE the destination.
// If you want to append, add StandardOpenOption.APPEND.
// See javadoc for more details.
final BufferedWriter writer = Files.newBufferedWriter(csv,
StandardCharsets.UTF_8);
) {
writer.write(s);
writer.newLine();
writer.flush();
} catch (IOException e) {
throw new RuntimeException("write failure", e);
}
}

File Montoring | Not updating in jsp while listening

I am monitoring the file creation and modification in directory using commons-io jar.I am able to get the results in Eclipse console.
final long pollingInterval = 5 * 1000;
String FOLDER = "C:/test";
File folder = new File(FOLDER);
folder.setReadable(true);
if (!folder.exists()) {
// Test to see if monitored folder exists
throw new RuntimeException("Directory not found: " + FOLDER);
}
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor =
new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
// Is triggered when a file is created in the monitored folder
#Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
System.out.println("File created: "
+ file.getCanonicalPath());
getNewMethod(file);// here in this method i am not able to return since its void.
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
#Override
public void onFileChange(File file) {
try {
System.out.println("File modified: "
+ file.getCanonicalPath());
getNewMethod(file); // here in this method i am not able to return since its void.
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
The problem is that i cannot able to return the file name when it is calling onFileCreate and onFileChange methods.How to achieve this? And also i am trying to call one method inside onFileCreate and onFileChange which returns a list.How to return the list? Because in this listener i dont see return parameter except void.
//Calling a newMethod
public String getNewMethod(File newfile) throws IOException{
System.out.println("getList method called : "+newfile.getCanonicalPath());
return "redirect:finalPage"; // here the redirection is not happening
}
When i see file change/create event is triggered, i need the changes to be update in jsp.How to achieve this?
You can create File object before you create listener and set file in onFileCreate() method. You can also call the methods within anonymous class methods by using OuterClass.this.OuterClassMethod().
File file;
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
// Is triggered when a file is created in the monitored folder
#Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
this.file = file;
System.out.println("File created: "
+ file.getCanonicalPath());
OuterClassName.this.newMethod();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
#Override
public void onFileChange(File file) {
try {
System.out.println("File modified: "
+ file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
};
public list newMethod(){
// returns list here
List list = new ArrayList();
return list;
}
Note: Replace OuterClassName with actual outer class name.
import java.io.File;
public class Test {
public void testMonitoring {
final long pollingInterval = 5 * 1000;
String FOLDER = "C:/test";
File folder = new File(FOLDER);
folder.setReadable(true);
if (!folder.exists()) {
// Test to see if monitored folder exists
throw new RuntimeException("Directory not found: " + FOLDER);
}
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor()
{
// Is triggered when a file is created in the monitored folder
private String filePath;
#Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
System.out.println("File created: "
+ file.getCanonicalPath());
setFilePath(file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
#Override
public void onFileChange(File file) {
try {
System.out.println("File modified: "
+ file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
/**
* #return the filePath
*/
public String getFilePath() {
return filePath;
}
/**
* #param filePath the filePath to set
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
}
};
observer.addListener(listener);
//getting the file path
System.out.println(listener.getFilePath());
monitor.addObserver(observer);
monitor.start();
}
}

file.delete() wont delete file, java

Okay, this is going to be a bit long. So I made a junit test class to test my program. I wanted to test if a method that uses a Scanner to read a file into the program threw and exception, if the file didn't exist like this:
#Test
public void testLoadAsTextFileNotFound()
{
File fileToDelete = new File("StoredWebPage.txt");
if(fileToDelete.delete()==false) {
System.out.println("testLoadAsTextFileNotFound - failed");
fail("Could not delete file");
}
try{
assertTrue(tester.loadAsText() == 1);
System.out.println("testLoadAsTextFileNotFound - passed");
} catch(AssertionError e) {
System.out.println("testLoadAsTextFileNotFound - failed");
fail("Did not catch Exception");
}
}
But the test fails at "could not delete file", so I did some searching. The path is correct, I have permissions to the file because the program made it in the first place. So the only other option would be, that a stream to or from the file is still running. So I checked the method, and the other method that uses the file, and as far as I can, both streams are closed inside the methods.
protected String storedSite; //an instance variable
/**
* Store the instance variable as text in a file
*/
public void storeAsText()
{
PrintStream fileOut = null;
try{
File file = new File("StoredWebPage.txt");
if (!file.exists()) {
file.createNewFile();
}
fileOut = new PrintStream("StoredWebPage.txt");
fileOut.print(storedSite);
fileOut.flush();
fileOut.close();
} catch(Exception e) {
if(e instanceof FileNotFoundException) {
System.out.println("File not found");
}
fileOut.close();
} finally {
if(fileOut != null)
fileOut.close();
}
}
/**
* Loads the file into the program
*/
public int loadAsText()
{
storedSite = ""; //cleansing storedSite before new webpage is stored
Scanner fileLoader = null;
try {
fileLoader = new Scanner(new File("StoredWebPage.txt"));
String inputLine;
while((inputLine = fileLoader.nextLine()) != null)
storedSite = storedSite+inputLine;
fileLoader.close();
} catch(Exception e) {
if(e instanceof FileNotFoundException) {
System.out.println("File not found");
return 1;
}
System.out.println("an Exception was caught");
fileLoader.close();
} finally {
if(fileLoader!=null)
fileLoader.close();
}
return 0; //return value is for testing purposes only
}
I'm out of ideas. Why can't I delete my file?
EDIT: i've edited the code, but still this give me the same problem :S
You have two problems here. The first is that if an exception is thrown during your write to the file, the output stream is not closed (same for the read):
try {
OutputStream someOutput = /* a new stream */;
/* write */
someOutput.close();
The second problem is that if there's an exception you aren't notified:
} catch (Exception e) {
if (e instanceof FileNotFoundException) {
/* do something */
}
/* else eat it */
}
So the problem is almost certainly that some other exception is being thrown and you don't know about it.
The 'correct' idiom to close a stream is the following:
OutputStream someOutput = null;
try {
someOutput = /* a new stream */;
/* write */
} catch (Exception e) {
/* and do something with ALL exceptions */
} finally {
if (someOutput != null) someOutput.close();
}
Or in Java 7 you can use try-with-resources.

Renaming a file on HDFS works in local mode but not in cluster mode

I have an object in charge of opening a file on HDFS to write. This object renames the file it just wrote once the close() method is invoked.
The mechanism works when running in local mode, but it fails to rename the file in cluster mode.
//Constructor
public WriteStream() {
path = String.format("in_progress/file");
try {
OutputStream outputStream = fileSystem.create(new Path(hdfs_path+path), new Progressable() {public void progress() { System.out.print("."); }
});
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Did you experience that before ?
Apparently FileSystem.rename(Path) creates missing directories on the path when executed in local mode, but it does not when run in cluster mode.
This code works in both modes:
public void close() {
String dirPath = String.format("%s_dir/", date, timestamp);
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.mkdir(new Path(hdfs_path+dirPath));
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Just curious, but how can you rename a file that officially doesn't exist (because you're still writing at that point)?
The fix is to rename after the file has been completed. That is, when you invoked the close method.
So your code should look like this:
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
writer.close();
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
} catch (IOException e) {
e.printStackTrace();
}
}

Categories

Resources