I have already surveyed SO for an answer, and could not find an appropriate one.
When I launch my program from a jar I need to create a folder in the directory where the jar file is located. It should not matter where the user saves the jar file.
Here is the newest code I was playing with: A System.out.println will print out the correct directory but the folder will not be created. In contrast,everything is being saved to my System32 folder as of now.
public static String getProgramPath() throws IOException{
String currentdir = System.getProperty("user.dir");
currentdir = currentdir.replace( "\\", "/" );
return currentdir;
}
File dir = new File(getProgramPath() + "Comics/");//The name of the directory to create
dir.mkdir();//Creates the directory
To get a Jar's path can be a little trickier than simply getting the user.dir directory. I can't remember the details why, but user.dir does not return this path reliably in all situations. If you absolutely must get the jar's path, then you need to do a little black magic and first get the class's protectionDomain. Something like:
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import javax.swing.JOptionPane;
public class MkDirForMe {
public static void main(String[] args) {
try {
String path = getProgramPath2();
String fileSeparator = System.getProperty("file.separator");
String newDir = path + fileSeparator + "newDir2" + fileSeparator;
JOptionPane.showMessageDialog(null, newDir);
File file = new File(newDir);
file.mkdir();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProgramPath2() throws UnsupportedEncodingException {
URL url = MkDirForMe.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = URLDecoder.decode(url.getFile(), "UTF-8");
String parentPath = new File(jarPath).getParentFile().getPath();
return parentPath;
}
}
Even this isn't guaranteed to work, and you'll have to resign yourself to the fact that there are just some times (for instance for security reasons) when you won't be able to get a Jar's path.
With some changes (such as adding a "/" before Comics), I managed to create the directory where you expected it to. Here is the full code I used.
import java.io.*;
public class TestClass {
public static String getProgramPath() throws IOException{
String currentdir = System.getProperty("user.dir");
currentdir = currentdir.replace( "\\", "/" );
return currentdir;
}
public static void main(String[] argv) {
try {
String d = getProgramPath() + "/Comics/";
System.out.println("Making directory at " + d);
File dir = new File(d);//The name of the directory to create
dir.mkdir();//Creates the directory
}
catch (Exception e) { System.out.println("Exception occured" + e);}
}
}
In the future, please don't hard code things like "/" and such. Use built-in libraries which will ask the OS what is right in this case. This ensures the functionality doesn't break (as easily) cross platform.
Of course, catch the exception properly etc. This is just quick and dirty attempt to mold your code into something that works.
Related
Being very new to Java, I'm unable to bring a small concept to a syntactic form. Apologies.
My project structure looks like below & i'm trying to walk thru the sub folders of applications
directory & search for a folder named conduit, if present, create a new folder called base parallel to it.
At best I came up with the below code, post that, kind of struggling.
/home/project_A/applications
|sub_project_A
|target
|conduit
|sub_project_B
|target
|conduit
|sub_project_C
|target
|class
|sub_project_D
|target
|conduit
public class Test
{
public static void main(String[] args)
{
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
String appDir = s + "/applications";
System.out.println("Directory Exists" + appDir);
}
}
You can use BFS to traverse the sub directories:
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Queue;
public class MyClass {
public static void main(String[] args) {
Path currentRelativePath = Paths.get("");
Queue<Path> paths = new LinkedList<>();
paths.add(currentRelativePath);
while(!paths.isEmpty()) {
Path current = paths.poll();
File currentFile = current.toFile();
if(currentFile.isDirectory()) {
if(currentFile.getName().equals("conduit")) {
// Found the directory called conduit, Do what you have to do here
}else {
for(String fileName : currentFile.list()) {
paths.add(Paths.get(currentFile.getAbsolutePath()+"/"+fileName));
}
}
}
}
}
}
Using Files.find is a good way to traverse a series of directories quickly to find files or directories matching any criteria you need. Here is an example which prints off the matches:
BiPredicate<Path, BasicFileAttributes> predicate = (p,a) -> a.isDirectory() && "conduit".equals(p.getFileName().toString());
try(var dirs = Files.find(Path.of("."), Integer.MAX_VALUE, predicate)) {
dirs.forEach(p -> {
System.out.println("Found "+p+", create if not exists: "+p.resolveSibling("base"));
}
);
}
i want to get files inside a folder when my application is running, so i know that i need to get it as resouce, if i will get it as file, it wont work, so it what i did.
jaxbContext = JAXBContext.newInstance(Catalogo.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("catalogos/");
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
String line;
try {
while((line = br.readLine()) != null){
InputStream resourceAsStream1 = getClass().getClassLoader().getResourceAsStream("catalogos/"+line);
tempCat = (Catalogo) jaxbUnmarshaller.unmarshal(resourceAsStream1);
if(tempCat != null){
codigoCurso = String.valueOf(tempCat.getCourse().getId());
nomeDoCurso = dados.get(codigoCurso);
anoCatalogo = String.valueOf(tempCat.getAno());
if(nomeDoCurso == null){
dados.put(codigoCurso, tempCat.getCourse().getNome());
}
anos.add(anoCatalogo);
}
}
What i want to do is, get all files inside a folder (/catalogos/) and loop through and unmarshall each to an object so i will be able to access the property i need. So, when i run this with netbeans, works perfectly, but when i build and run the jar, i dont get the same result i've got using netbeans, i mean, the data is not where i expected.
The following example demonstrates how to get files from a directory in current runnable jar file and read these files contents.
Assume you have a NetBeans project with name "FolderTestApp". Do the following steps:
In your project root folder FolderTestApp\ create folder myFiles.
Copy your catalogos folder to the FolderTestApp\myFiles\
myFiles folder is necessary to preserve catalogos folder in your jar file structure when project jar is being generated. myFiles folder will disappear from jar file, but catalogos folder will remain.
If you don't do these steps, and place catalogos directly to the project folder (not as a child folder for myFiles), then your files from catalogos folder will be placed to the root of your jar file.
Add myFiles folder as a source folder in netbeans project properties.
Assume your property files contain the following contents:
file1.properties:
key11=value11
key12=value12
key13=value13
file2.properties:
key21=value21
key22=value22
key23=value23
Please note, that code below is not optimized. It is plain'n'dirty proof of concept to show, how to solve your task.
Add the following class to your project:
package folderapp;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class FolderTestApp {
public static void main(String[] args) throws URISyntaxException, IOException {
new FolderTestApp();
}
public FolderTestApp() throws URISyntaxException, IOException {
// determining the running jar file location
String jarFilePath = getClass().getProtectionDomain().
getCodeSource().getLocation().toURI().getPath();
// note, that the starting / is removed
// because zip entries won't start with this symbol
String zipEntryFolder = "catalogos/";
try (ZipInputStream zipInputStream
= new ZipInputStream(new FileInputStream(jarFilePath))) {
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
System.out.println("processing: " + zipEntry.getName());
if (zipEntry.getName().startsWith(zipEntryFolder)) {
// directory "catalogos" will appear as a zip-entry
// and we're checking this condition
if (!zipEntry.isDirectory()) {
// adding symbol / because it is required for getResourceAsStream() call
printProperties("/" + zipEntry.getName());
}
}
zipEntry = zipInputStream.getNextEntry();
}
}
}
public void printProperties(String path) throws IOException {
try (InputStream is = getClass().getResourceAsStream(path)) {
InputStreamReader fr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(fr);
Properties properties = new Properties();
properties.load(br);
System.out.println("contents from: " + path + "\n");
Set<Object> keySet = properties.keySet();
for (Object key : keySet) {
System.out.println(key + " = " + properties.get(key));
}
System.out.println("---------------------------------------");
}
}
}
Set this class as the main class in your project settings (Run section).
And build your project via menu: Run - Build.
As your project has been built, open FolderTestApp/dist folder, where your generated jar is located and run this jar file:
That's it :)
I have to move files from one directory to other directory.
Am using property file. So the source and destination path is stored in property file.
Am haivng property reader class also.
In my source directory am having lots of files. One file should move to other directory if its complete the operation.
File size is more than 500MB.
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import static java.nio.file.StandardCopyOption.*;
public class Main1
{
public static String primarydir="";
public static String secondarydir="";
public static void main(String[] argv)
throws Exception
{
primarydir=PropertyReader.getProperty("primarydir");
System.out.println(primarydir);
secondarydir=PropertyReader.getProperty("secondarydir");
File dir = new File(primarydir);
secondarydir=PropertyReader.getProperty("secondarydir");
String[] children = dir.list();
if (children == null)
{
System.out.println("does not exist or is not a directory");
}
else
{
for (int i = 0; i < children.length; i++)
{
String filename = children[i];
System.out.println(filename);
try
{
File oldFile = new File(primarydir,children[i]);
System.out.println( "Before Moving"+oldFile.getName());
if (oldFile.renameTo(new File(secondarydir+oldFile.getName())))
{
System.out.println("The file was moved successfully to the new folder");
}
else
{
System.out.println("The File was not moved.");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
System.out.println("ok");
}
}
}
My code is not moving the file into the correct path.
This is my property file
primarydir=C:/Desktop/A
secondarydir=D:/B
enter code here
Files should be in B drive. How to do? Any one can help me..!!
Change this:
oldFile.renameTo(new File(secondarydir+oldFile.getName()))
To this:
oldFile.renameTo(new File(secondarydir, oldFile.getName()))
It's best not to use string concatenation to join path segments, as the proper way to do it may be platform-dependent.
Edit: If you can use JDK 1.7 APIs, you can use Files.move() instead of File.renameTo()
Code - a java method:
/**
* copy by transfer, use this for cross partition copy,
* #param sFile source file,
* #param tFile target file,
* #throws IOException
*/
public static void copyByTransfer(File sFile, File tFile) throws IOException {
FileInputStream fInput = new FileInputStream(sFile);
FileOutputStream fOutput = new FileOutputStream(tFile);
FileChannel fReadChannel = fInput.getChannel();
FileChannel fWriteChannel = fOutput.getChannel();
fReadChannel.transferTo(0, fReadChannel.size(), fWriteChannel);
fReadChannel.close();
fWriteChannel.close();
fInput.close();
fOutput.close();
}
The method use nio, it make use os underling operation to improve performance.
Here is the import code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
If you are in eclipse, just use ctrl + shift + o.
I've tried directly linking using the entire path but that hasn't solved it either.
package eliza;
import java.io.*;
public class Eliza {
public static void main(String[] args) throws IOException {
String inputDatabase = "src/eliza/inputDataBase.txt";
String outputDatabase = "src/eliza/outputDataBase.txt";
Reader database = new Reader();
String[][] inputDB = database.Reader(inputDatabase);
String[][] outputDB = database.Reader(outputDatabase);
}
}
Here is the reader class:
package eliza;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Reader {
public String[][] Reader(String name) throws IOException {
int length = 0;
String sizeLine;
FileReader sizeReader = new FileReader(name);
BufferedReader sizeBuffer = new BufferedReader(sizeReader);
while((sizeLine = sizeBuffer.readLine()) != null) {
length++;
}
String[][] database = new String[length][1];
return (database);
}
}
Here's a photo of my directory. I even put these text files in the "eliza" root folder: here
Any ideas?
Since you are using an IDE, you need to give the complete canonical path. It should be
String inputDatabase = "C:\\Users\\Tommy\\Desktop\\Eliza\\src\\eliza\\inputDataBase.txt";
String outputDatabase = "C:\\Users\\Tommy\\Desktop\\Eliza\\src\\eliza\\outputDataBase.txt";
The IDE is probably executing the bytecode from its bin folder and cannot find the relative reference.
give the exact path like
String inputDatabase = "c:/java/src/eliza/inputDataBase.txt";
you have not given the correct path, Please re check
try
{BASE_PATH}+ "Eliza/src/inputDataBase.txt"
The source directory tree isn't generally present during execution, so files that are required at runtime shouldn't be put there ... unless you're going to use them as resources, in which case their pathname is relative to the package root, and does not begin with 'src', and the data is accessed by a getResourceXXX() method, not via a FileInputStream.
In my application, I need to create a representation of a directory which is the package where the <class_name> is contained. In short, I need to create a File object which represents that directory.
The code is as follows :
Package package1 = <class_name>.class.getPackage();
String string = "/" + package1.getName().replace('.', '/');
URL url = <class_name>.class.getResource( string );
File file = new File( url.toURI() );
Now, the problem is when creating the File object, this exception is thrown:
java.lang.IllegalArgumentException: URI is not hierarchical.
May anyone shed light and help me solve this?
I don't use NetBeans. So, I can't help you with that. But, if you can use java at the command line, then try using this test code.
package rick;
import java.net.*;
import java.io.*;
public class Test{
public static void main(String[] args){
Test test = new Test();
Package package1 = test.getClass().getPackage();
String string = "/" + package1.getName().replace('.','/');
URL url = test.getClass().getResource(string);
File file = new File(url.toString());
System.out.println(file.getPath());
}
}