Is there an error within the following code, It does not seem to be creating the text file (numbers.txt) that I am intending? I am aware that It is supossed to create the file in the JRE System library but I cannot locate it.
package test;
import java.io.FileNotFoundException;
import java.util.Formatter;
public class FileReaderTest {
private Formatter output;
/**
* default const
*/
public FileReaderTest() {
}
/**
* Method that enables a user to open a file
*/
public void openFile() {
try {
output = new Formatter("numbers.txt");
} catch (FileNotFoundException e) {
System.err.println("Problem found when opening file");
System.exit(1);// terminate
} catch (SecurityException Se) {
System.err.println("You dont have access to open file");
System.exit(1);// terminate
}
}// end of openFile
/**
* Method enabling user to write numbers to file
*/
public void writeToFile() {
// numbers to be written to file
Integer[] numbers = { 2, 5, 6, 7, 8, 9 };
for (Integer i : numbers) {
output.format("%d/n", i);
}
}// end of writeToFile
/**
* Method that closes file
*
*/
public void closeFile() {
if (output != null) {
output.close();
}
}// end of close file
}// class end
My implementation of the fileReaderTest in the Main:
public static void main(String[] args) {
//file input/putput testing
System.out.println("Opening file");
FileReaderTest file1= new FileReaderTest();
file1.openFile();//opening the file
file1.writeToFile();//writing values within the file
file1.closeFile();//closing the file
System.out.println("Finished with file");
}
}
I Think you should use File.getAbsolutePath()
On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.
your current execution dir is: new File(".").getAbsolutePath(), check there
Related
When my application starts it reads a configuration properties file using the following code:
Properties properties = new Properties();
// parse the config resource
try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename))
{
if (input == null)
{
// throw exception
}
// read the property list (key-value pairs) from the input byte stream
properties.load(input);
}
I am able to read and set individual properties.
The properties file is located in src/main/resources and after I build the application using maven, a copy of it is placed in target/classes. The jar file that is created also has a copy of it in the root directory when I open it up.
I would also like to be able to overwrite the properties file so that next time the application starts up, then it will read the new updated file. How do I achieve this? Is it even possible?
I found this question but no answers.
I've tried this:
try (OutputStream output = new FileOutputStream(filename))
{
properties.store(output, null);
}
which works if I just want to create a new file altogether. I would then have to modify the application so that it reads from a given folder rather than what originated from the resources folder. Is this what I should be doing?
I'm fairly new to Java so please go easy.
Storing the initial, default properties in the jar file, as resources is fine.
But if you want them to be writable, then you need to really store them as a file somewhere on the disk. Typically, under a .yourapp directory (or in a .yourapp file) inside the user's home directory.
So, try finding the file, and if not present, fallback to the resources. When writing, always write to the file.
This is an example code you can use for this. You create a config folder in the project root directory, an inside it you place your app.properties file
package com.yourparckage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
/* Create basic object */
private ClassLoader objClassLoader = null;
private Properties commonProperties = new Properties();
public static final String CONFIG_FILE = "config/app.properties";
/**
* This method loads config data from properties file
*
*/
public Config() {
objClassLoader = getClass().getClassLoader();
}
public String readKey(String propertiesFilename, String key)
{
/* Simple validation */
if (propertiesFilename != null && !propertiesFilename.trim().isEmpty() && key != null
&& !key.trim().isEmpty()) {
/* Create an object of FileInputStream */
InputStream objFileInputStream = null;
/**
* Following try-catch is used to support upto 1.6. Use try-with-resource in JDK
* 1.7 or above
*/
try {
/* Read file from resources folder */
objFileInputStream = new FileInputStream(propertiesFilename);
/* Load file into commonProperties */
commonProperties.load(objFileInputStream);
/* Get the value of key */
return String.valueOf(commonProperties.get(key));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
/* Close the resource */
if (objFileInputStream != null) {
try {
objFileInputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
return null;
}
}
I am trying to move directory forcefully that means if already exist then overwrite without asking.
Code :
import java.io.IOException;
import java.lang.System;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class MoveDirectory {
private static void movefilesandfolder(String sourceFilevar,String destinationFilevar)
{
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Sole entry point to the class and application.
* #param args Array of String arguments.
*/
public static void main(String[] args) {
movefilesandfolder("C:\\FTPDownload\\Downloaded\\epi141225_0001","C:\\FTPDownload\\In_Progress\\epi141225_0001");
}
}
Error output:
source=C:\FTPDownload\Downloaded\epi141225_0001
destination=C:\FTPDownload\In_Progress\epi141225_0001
java.nio.file.DirectoryNotEmptyException: C:\FTPDownload\In_Progress\epi141225_0001
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:372)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:286)
at java.nio.file.Files.move(Files.java:1345)
at MoveDirectory.movefilesandfolder(MoveDirectory.java:22)
at MoveDirectory.main(MoveDirectory.java:36)
This code works if Folder not present on destination path but fails if already exist. This code doesn't work even if empty folder is present on destination path.
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
In this call option i am using is not working for folder. This code Tested for file and it worked for already exist file as well.
But I want to move/overwrite folder.
You can delete destination directory before moving, or, if you want to merge directories together, loop your directory files and move anyone in new folder
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
if(new File(destinationFile).exists()){
// DELETE DIRECTORY
}
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
or
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
if(new File(destinationFile).exists()){
// for each file in sourceFile
// Files.move file ...
}else{
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
javadoc:
public static Path move(Path source,
Path target,
CopyOption... options)
throws IOException
Move or rename a file to a target file.
By default, this method attempts to move the file to the target file, failing if the target file exists except if the source and target are the same file, in which case this method has no effect. If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved. This method may be invoked to move an empty directory.
You can try using Path.resolve(other Path) method like this
Files.move(sourceFile, destinationFile.resolve(Paths.get(sourceFile).getFileName()), StandardCopyOption.REPLACE_EXISTING)
Java docs, give the best explanation about this.
I'm trying to copy a file with java.nio.file.Files like this:
Files.copy(cfgFilePath, strTarget, StandardCopyOption.REPLACE_EXISTING);
The problem is that Eclipse says "The method copy(Path, Path, CopyOption...) in the type Files is not applicable for the arguments (File, String, StandardCopyOption)"
I'm using Eclipse and Java 7 on Win7 x64. My project is set up to use Java 1.6 compatibility.
Is there a solution to this or do I have to create something like this as a workaround:
File temp = new File(target);
if(temp.exists())
temp.delete();
Thanks.
You need to pass Path arguments as explained by the error message:
Path from = cfgFilePath.toPath(); //convert from File to Path
Path to = Paths.get(strTarget); //convert from String to Path
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
That assumes your strTarget is a valid path.
As a complement to #assylias' answer:
If you use Java 7, drop File entirely. What you want is Path instead.
And to get a Path object matching a path on your filesystem, you do:
Paths.get("path/to/file"); // argument may also be absolute
Get used to it real fast. Note that if you still use APIs which require File, Path has a .toFile() method.
Note that if you are in the unfortunate case where you use an API which returns File objects, you can always do:
theFileObject.toPath()
But in code of yours, use Path. Systematically. Without a second thought.
EDIT Copying a file to another using 1.6 using NIO can be done as such; note that the Closer class is inspited by Guava:
public final class Closer
implements Closeable
{
private final List<Closeable> closeables = new ArrayList<Closeable>();
// #Nullable is a JSR 305 annotation
public <T extends Closeable> T add(#Nullable final T closeable)
{
closeables.add(closeable);
return closeable;
}
public void closeQuietly()
{
try {
close();
} catch (IOException ignored) {
}
}
#Override
public void close()
throws IOException
{
IOException toThrow = null;
final List<Closeable> l = new ArrayList<Closeable>(closeables);
Collections.reverse(l);
for (final Closeable closeable: l) {
if (closeable == null)
continue;
try {
closeable.close();
} catch (IOException e) {
if (toThrow == null)
toThrow = e;
}
}
if (toThrow != null)
throw toThrow;
}
}
// Copy one file to another using NIO
public static void doCopy(final File source, final File destination)
throws IOException
{
final Closer closer = new Closer();
final RandomAccessFile src, dst;
final FileChannel in, out;
try {
src = closer.add(new RandomAccessFile(source.getCanonicalFile(), "r");
dst = closer.add(new RandomAccessFile(destination.getCanonicalFile(), "rw");
in = closer.add(src.getChannel());
out = closer.add(dst.getChannel());
in.transferTo(0L, in.size(), out);
out.force(false);
} finally {
closer.close();
}
}
package main.java;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class CopyFileOnExist {
public static void main(String[] args) {
Path sourceDirectory = Paths.get("C:/Users/abc/Downloads/FileNotFoundExceptionExample/append.txt");
Path targetDirectory = Paths.get("C:/Users/abc/Downloads/FileNotFoundExceptionExample/append5.txt");
//copy source to target using Files Class
try {
Files.copy(sourceDirectory, targetDirectory,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
strTarget is a "String" object and not a "Path" object
I am playing a bit with the new Java 7 IO features. Actually I am trying to retrieve all the XML files in a folder. However this throws an exception when the folder does not exist. How can I check if the folder exists using the new IO?
public UpdateHandler(String release) {
log.info("searching for configuration files in folder " + release);
Path releaseFolder = Paths.get(release);
try(DirectoryStream<Path> stream = Files.newDirectoryStream(releaseFolder, "*.xml")){
for (Path entry: stream){
log.info("working on file " + entry.getFileName());
}
}
catch (IOException e){
log.error("error while retrieving update configuration files " + e.getMessage());
}
}
Using java.nio.file.Files:
Path path = ...;
if (Files.exists(path)) {
// ...
}
You can optionally pass this method LinkOption values:
if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
There's also a method notExists:
if (Files.notExists(path)) {
Quite simple:
new File("/Path/To/File/or/Directory").exists();
And if you want to be certain it is a directory:
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}
To check if a directory exists with the new IO:
if (Files.isDirectory(Paths.get("directory"))) {
...
}
isDirectory returns true if the file is a directory; false if the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
See: documentation.
Generate a file from the string of your folder directory
String path="Folder directory";
File file = new File(path);
and use method exist.
If you want to generate the folder you sould use mkdir()
if (!file.exists()) {
System.out.print("No Folder");
file.mkdir();
System.out.print("Folder created");
}
You need to transform your Path into a File and test for existence:
for(Path entry: stream){
if(entry.toFile().exists()){
log.info("working on file " + entry.getFileName());
}
}
There is no need to separately call the exists() method, as isDirectory() implicitly checks whether the directory exists or not.
import java.io.File;
import java.nio.file.Paths;
public class Test
{
public static void main(String[] args)
{
File file = new File("C:\\Temp");
System.out.println("File Folder Exist" + isFileDirectoryExists(file));
System.out.println("Directory Exists" + isDirectoryExists("C:\\Temp"));
}
public static boolean isFileDirectoryExists(File file)
{
if (file.exists())
{
return true;
}
return false;
}
public static boolean isDirectoryExists(String directoryPath)
{
if (!Paths.get(directoryPath).toFile().isDirectory())
{
return false;
}
return true;
}
}
We can check files and thire Folders.
import java.io.*;
public class fileCheck
{
public static void main(String arg[])
{
File f = new File("C:/AMD");
if (f.exists() && f.isDirectory()) {
System.out.println("Exists");
//if the file is present then it will show the msg
}
else{
System.out.println("NOT Exists");
//if the file is Not present then it will show the msg
}
}
}
File sourceLoc=new File("/a/b/c/folderName");
boolean isFolderExisted=false;
sourceLoc.exists()==true?sourceLoc.isDirectory()==true?isFolderExisted=true:isFolderExisted=false:isFolderExisted=false;
From SonarLint, if you already have the path, use path.toFile().exists() instead of Files.exists for better performance.
The Files.exists method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.
The same goes for Files.notExists, Files.isDirectory and Files.isRegularFile.
Noncompliant Code Example:
Path myPath;
if(java.nio.Files.exists(myPath)) { // Noncompliant
// do something
}
Compliant Solution:
Path myPath;
if(myPath.toFile().exists())) {
// do something
}
I have an exe process that is running with a shortcut.
In the "Start in" property of the shortcut I set it to the folder where all app resources are. The process still looks for files at the location of the exe and not the location written in the shortcut.
I can also see it in Process Explorer - the "current directory" is the location of the exe.
Is there a way to change it?
(If I wasn't clear enough -
I want to put my app in a central network location and not in each user folder - but I want it to run - above each user folder by putting a shortcut in each user folder.)
BTW : Why don't I solve it with code writing? Because of third party jars I have in my exe (I am using exe4j to make an exe)
From exe4-j documentation.., it seems this can be configured in exe4j project.
Working directory
For some applications (especially GUI applications) you might want to change the working directory
to a specific directory relative to the executable, for example to read config files that are in a fixed
location. To do so, please select the Change working directory to: checkbox and enter a
directory relative to the executable in the adjacent text field. To change the current directory to the
same directory where the executable is located, please enter a single dot.
One alternative is to use a System Property. Just create a shortcut like this:
java -Dmyproperty="\\myserver\myfolder" -jar yourjar.jar
And get this property on your program:
System.getProperty("myproperty");
You can also set multiple System Properties.
I would start the java application via a cmd or bat file, then change to the work dir before you call javaw. If you don't do any thing special in your java application code all the paths in it will be relative to the place where you started java.
Jess
You can hack the classpath programatically which would allow you to specify a specific folder or series of folders to access the data.
import java.io.IOException;
import java.io.File;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Method;
public class ClassPathHacker {
private static final Class[] parameters = new Class[]{URL.class};
public static void addFile(String s) throws IOException {
File f = new File(s);
addFile(f);
}//end method
public static void addFile(File f) throws IOException {
addURL(f.toURI().toURL());
}//end method
public static void addURL(URL u) throws IOException {
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[]{u});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}//end try catch
}//end method
}//end class
with the property loader file of
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
public abstract class PropertyLoader
{
/**
* Looks up a resource named 'name' in the classpath. The resource must map
* to a file with .properties extention. The name is assumed to be absolute
* and can use either "/" or "." for package segment separation with an
* optional leading "/" and optional ".properties" suffix. Thus, the
* following names refer to the same resource:
* <pre>
* some.pkg.Resource
* some.pkg.Resource.properties
* some/pkg/Resource
* some/pkg/Resource.properties
* /some/pkg/Resource
* /some/pkg/Resource.properties
* </pre>
*
* #param name classpath resource name [may not be null]
* #param loader classloader through which to load the resource [null
* is equivalent to the application loader]
*
* #return resource converted to java.util.Properties [may be null if the
* resource was not found and THROW_ON_LOAD_FAILURE is false]
* #throws IllegalArgumentException if the resource was not found and
* THROW_ON_LOAD_FAILURE is true
*/
public static Properties loadProperties (String name, ClassLoader loader)
{
if (name == null)
throw new IllegalArgumentException ("null input: name");
if (name.startsWith ("/"))
name = name.substring (1);
if (name.endsWith (SUFFIX))
name = name.substring (0, name.length () - SUFFIX.length ());
Properties result = null;
InputStream in = null;
try
{
if (loader == null) loader = ClassLoader.getSystemClassLoader ();
if (LOAD_AS_RESOURCE_BUNDLE)
{
name = name.replace ('/', '.');
// Throws MissingResourceException on lookup failures:
final ResourceBundle rb = ResourceBundle.getBundle (name,
Locale.getDefault (), loader);
result = new Properties ();
for (Enumeration keys = rb.getKeys (); keys.hasMoreElements ();)
{
final String key = (String) keys.nextElement ();
final String value = rb.getString (key);
result.put (key, value);
}
}
else
{
name = name.replace ('.', '/');
if (! name.endsWith (SUFFIX))
name = name.concat (SUFFIX);
// Returns null on lookup failures:
in = loader.getResourceAsStream(name);
if (in != null)
{
result = new Properties ();
result.load (in); // Can throw IOException
}
}
}
catch (Exception e)
{
result = null;
}
finally
{
if (in != null) try { in.close (); } catch (Throwable ignore) {}
}
if (THROW_ON_LOAD_FAILURE && (result == null))
{
throw new IllegalArgumentException ("could not load [" + name + "]"+
" as " + (LOAD_AS_RESOURCE_BUNDLE
? "a resource bundle"
: "a classloader resource"));
}
return result;
}
/**
* A convenience overload of {#link #loadProperties(String, ClassLoader)}
* that uses the current thread's context classloader.
*/
public static Properties loadProperties (final String name)
{
return loadProperties (name,
Thread.currentThread ().getContextClassLoader ());
}
private static final boolean THROW_ON_LOAD_FAILURE = true;
private static final boolean LOAD_AS_RESOURCE_BUNDLE = false;
private static final String SUFFIX = ".properties";
} // End of class
then you can add a path as follows
try {
//First Load up the properties and populate the config
ClassPathHacker.addFile("/pathtomyapp");
} catch (IOException ex) {
ex.printStackTrace();
}
properties = PropertyLoader.loadProperties("myapp");
or you can also use getResourceBundle to get your resources, this is just one example of hacking the classpath to allow files to be available, you can always just add the classpath programatically and let the jar files you need to be available to reside there, so if you always ensure that the app network path is Q: you can add Q:\ to the classpath.