File Directory and File Creation Problems - java

I have problems with the following code. I am trying to look at an existing directory to see if a file exists before creating a new one, but it doesnt seem to create a new file even though there are no existing ones in the directory. I have attached the two relevant methods, but the problem lies with the writeFile() method. I tried to use the existing 'dir' in writeFile, this didnt do the trick either. The rest of the program does seem to work, just the writeFile method has problems.
public void writeFile(String t) throws IOException {
File temp1 = new File(dateNow + File.separator + "Temperature.txt");
boolean check = temp1.exists();
if (!check)
newFiles();
}
public void newFiles() {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
dateNow = formatter.format(now.getTime());
System.out.println(dateNow);
// if (hour == 00 && mini == 00 && sec == 00) {
try {
dir = new File(dateNow);
boolean x = dir.mkdir();
// ....
} catch (Exception e) {
//
}
}

you need to specify the full path, not just the directory and the file name to create a file or even check it's existence,.
thank you for michael667, you remind me about the relative position is right,. :)
and there should be no problem with the above code,.

Related

Wildfly is keeping file using when I do Files.write

I have two method - one to write, second to rename file:
public void writeToFile(File file, String content, boolean isLastLine) {
Optional<File> optionalFile = Optional.ofNullable(file);
if (!isLastLine)
content += System.lineSeparator();
try {
Files.write(
optionalFile.orElseThrow(() -> new RuntimeException("File couldn't be find")).toPath(),
content.getBytes(),
StandardOpenOption.APPEND, StandardOpenOption.SYNC);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void renameFile(File fileToRename, String newFileName) {
Optional<File> optionalFile = Optional.ofNullable(fileToRename);
File finalBikFileName = new File(newFileName);
if (!optionalFile.orElseThrow(() -> new RuntimeException("File couldn't be find or doesn't exist")).renameTo(finalBikFileName)) {
throw new RuntimeException("File couldn't be saved - already exists or some other issues");
}
}
public void renameFile(File fileToRename, String newFileName) {
Optional<File> optionalFile = Optional.ofNullable(fileToRename);
File finalBikFileName = new File(newFileName);
if (!optionalFile.orElseThrow(() -> new RuntimeException("File couldn't be find or doesn't exist")).renameTo(finalBikFileName)) {
throw new RuntimeException("File couldn't be saved - already exists or some other issues");
}
}
This is normal class, in application deployed on wildfly. I tested it in many ways. If I comment the write function then rename function is working proper. But if I first write something to file and then I want to rename then i got "action cannot be completed because the file is open in another program" Also i cant touch this file in windows explorer - i can't rename or delete. What can be a reason? How can I unlock it?
1) Is it different threads (or server requests) that call the writeToFile and the renameFile methods? Or both methods are calling one after other under same thread/request?
2) How much data (content.length I mean) are you writing? Just want to make sure SYNC is done before the RENAME.

Error in Renaming Files

Although there are many questions here that involve errors with naming files, the code below tries to implement those examples but fails to actually change the name of the file in the system for no apparent reason.
public static void main(String[] args)
{
File dir = new File("E:/Vglut2 pharmacology/60730/");
File[] directoryListing = dir.listFiles();
for (File filius : directoryListing) {
try
{
String oldName = filius.getName();
int imgNum = returnImageNumber(oldName);
double imgMag = returnImageMagnification(oldName);
String newName = oldName.substring(0, oldName.indexOf('_')).concat('_' + String.valueOf(imgNum)).concat('_' + String.valueOf(imgMag));
File nova = new File(newName);
filius.renameTo(nova);
System.out.println(newName);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The resulting string that I tested by printing offers the desired resulting final name, however, in the end, the filesystem remains unchanged. A similar example from a previous stack overflow question is as seen below-
File dir = new File("D:/xyz");
if (dir.isDirectory()) { // make sure it's a directory
for (final File f : dir.listFiles()) {
try {
File newfile =new File("newfile.txt");
if(f.renameTo(newfile)){
System.out.println("Rename succesful");
}else{
System.out.println("Rename failed");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
As seen there are no significant differences that I can tell which would affect the viability of this process. Note I am running Windows 10 Home edition as an admin if this is related. Thanks for the help.
filius.getName() only gets the last part of the files path. renameTo on the other hand needs the full path. So in fact you end up trying to move your file into a different directory.
Use:
File nova = new File(dir, newName);
in order to rename the file in the original directory.

file.exists return false when folder is not empty

I am trying to access a file in remote shared location.
////hostname//data//upload//123//test.txt
File sourceFile=new File("////hostname//data//upload//123//test.txt");
sysout("sourceFile.exists()"+sourceFile.exists())//returning false
If a directory is empty file.exists() is returning true.
I am using Java 1.6
I don't understand what is this weird behavior.
First of all to come back to Erwin´s suggestion, this is not the right attempt. The character \ is used in Java as an escape sequence, usually to print out reserved characters. For example will
String s = "The weather is really "nice" today";
result in an error, as " is already reserved for strings. The correct version would be
String s = "The weather is really \"nice\" today";
Coming back to the question, you have to know that when you create a file and test if it exists Java will validate the abstract pathname of the file. That said, if your abstact path is a directory and it exists true will be returned.
Edit:
If you intend to check if an abstract pathname is a directory try the following:
// Check if a file is a directory
if(file.isDirectory()) {
}
// Check if a file contains something
if(file.list().length > 0) {
}
Check this example ,it checks the directory else creates a new one then your new file created.
File f = new File("D:/image_send");
File file = new File("D:/image_send/" + date + ".txt");
try {
if(!f.isDirectory()){
f.mkdirs();
}
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("File created Success");
public static boolean fileTransfer(String src, String des) throws Exception {
if (des == null || des.equals("") || src == null || src.equals("")) {
return false;
}
File fileExisting = new File(src);
File fileNew = new File(des+ fileExisting.getName());
if (fileExisting.exists() && !fileExisting.isDirectory()) {
if (fileExisting.renameTo(fileNew)) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
}
return fileNew.exists();
}
This is the code for file transfer as per your comment ,use src as sourcepath and des as destination path if you get a boolean false,that means path given is wrong.

Odd isDirectory behaviour

I've looked around at similar questions on here but none of the answers I have attempted have proven useful. I am attempting to load a directory that exists on the web. When I navigate to the path that my program is generating, it is definitely a directory. However, when I attempt to use isDirectory on the file object representing the path, it doesn't work.
Snippet where I Generate the file
if (sport == null || sport.trim().length() == 0) {
return null;
}
File dayDirectory = new File(aggregatorRootDirectory, new SimpleDateFormat("yyyy-MM-dd").format(date));
String sportDirectory = sport.replace(",", "");
return new File(dayDirectory + "\\" + sportDirectory + "\\");
The path that this is generating is valid, and is definitely a directory. The directory is a website link, if that makes a huge difference?
Snippet where I use the file
try {
if (directory == null || !directory.getCanonicalFile().isDirectory() ||
fileNamePattern == null || fileNamePattern.trim().length() == 0) {
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
File[] files = directory.listFiles();
This attempt is throwing a java.io.IOException with the message:
The filename, directory name, or volume label syntax is incorrect.
The error is occuring in the if statement, when I attempt to call getCanonicalFile().
When I remove the getCanonicalFile() method call, it resolves to false when I call isDirectory.
If I remove the check all together, listFiles() resolves to null, which is making me think there's something more to this.
Is there a common issue with isDirectory and web links, or is there a way to force a file object to intepret a path as a directory?
Edit
Below is the Scala code that performed the desired functionality. The following function grabbed the list of files from the online directory:
private def getFiles(directory: File, fileNamePattern: String): Seq[Elem] = {
if(directory == null || ! directory.isDirectory
|| fileNamePattern == null || fileNamePattern.trim.length == 0) {
return Nil
}
val filesList = directory.listFiles( new FilenameFilter {
override def accept(dir: File, name: String) = { name.matches(fileNamePattern)
} } )
val sortedFilesList = filesList.sortBy(_.lastModified)
val feedsList = mutable.ListBuffer[Elem]()
for(file <- sortedFilesList) {
try {
feedsList += XML.loadFile(file) % new UnprefixedAttribute("original-filePath", file.getCanonicalPath, Null)
}
catch {
case _ => // TODO log
}
}
feedsList
}
And this function created a new File object from said directory.
private def getSportDirectory(sport: String, date: Date = new Date): File = {
if(sport == null || sport.trim.length == 0) {
return null;
}
val dayDirectory = new File(aggregatorRootDirectory, new SimpleDateFormat("yyyy-MM-dd").format(date))
val sportDirectory = sport.replace(",", "") // resolving sports like "HR,DG" to "HRDG". Not ideal but ...
new File(dayDirectory, sportDirectory)
}
The directory is a website link, if that makes a huge difference?
Yes, it absolutely does. File is intended for file systems - not HTTP.
Basically, you shouldn't be using File* classes if you're trying to do things with web resources. Just because various applications (e.g. Windows Explorer) try to hide the differences between the two doesn't mean you can always do so in code.
For example, I don't believe that there is a generic HTTP equivalent of "list files" for a directory. You request a specific resource - and that may return a directory listing, but it could equally give the default page for a directory.

Odd Bug , using File.RenameTo()

I have a program in which i must rename a set of folders. they are all in "ID [Name]" format, and I want to rename them to "Name [ID]". (Its more of a training for me, for learning java))
the problem is, if the number of folders it must rename go beyond 20-24 . the program won't work, and will give the files faulty names. (the renaming process succeeds, but names are wrong)
but if they are below 20 folders, it works perfectly.(tested with the same folders)
here's the whole code:
public class DirRename {
private String parentDir;
private DirectoryStream<Path> fileList;
public DirRename(final Path dir)
{
parentDir = dir.toString();
if(!Files.exists(dir) || !Files.isDirectory(dir) || !Files.isReadable(dir))
System.out.println("Directory Read Error!!!");
//filter to return only directories in parent folder
DirectoryStream.Filter<Path> dirOnlyFilter =
new DirectoryStream.Filter<Path>() {
public boolean accept(Path file) throws IOException {
return (Files.isDirectory(file));
}
};
try
{
fileList = Files.newDirectoryStream(dir,dirOnlyFilter);
}
catch(IOException | DirectoryIteratorException x)
{
System.err.println(x);
}
}
public void rename()
{
for(Path filepath : fileList)
{
String name = filepath.getFileName().toString();
File inFile = filepath.toFile();
if(!inFile.exists() || !inFile.isDirectory() || !inFile.canWrite())
{
System.out.println("Directory is not writeable");
return;
}
Pattern regex = Pattern.compile("((?:[\\w\\d]*(?:\\s|-){0,2}[\\w\\d]+)*)\\s*-*\\s*(?:\\[|\\Q(\\E)(.+)(?:\\]|\\Q)\\E)$");
Matcher match = regex.matcher(name);
while(match.find())
{
String gameID = match.group(1);
String gameName = match.group(2);
String rename = parentDir+File.separator+gameName+" ["+gameID+"]";
File toFile = new File(rename);
if(!Paths.get(rename).isAbsolute())
{
System.out.println("Cannot rename "+name+"to "+rename);
return;
}
if(inFile.renameTo(toFile))
System.out.println("Success!");
else
System.out.println("Renaming Failed!!! for "+rename);
}
}
}
}
I tried checking the names with "system.out.println(toFile.getName())" while deleting the line "inFile.renameTo(toFile)". all names were correct.
but when i added that line back, the same names were printed incorrectly.(although some that were printed correctly were renamed incorrectly)
I'm completely confused. and I'm new to java, and generally less than a noob programmer. can someone please tell me what's going on?
Many thanks
EDIT: I found the problem.the loop:
for(Path filepath : fileList){}
runs 116 times while i only have 64 folders. I can't find any explanation as to why this happens, I use the same loop to print folder names only in the following function and it runs exactly 64 times.( exactly the number of folders I have)
public void printFolders()
{
for(Path filepath : fileList)
System.out.println(filepath.getFileName());
}
okay I finally Fixed my own problem. here's my guess on why this happened (I don't know the inner working of DirectoryStream so its just a guess).
when the folders were more than a few, the stream would read the previously renamed folders and add them as new folders, thus they were getting renamed twice. either changing the name back to original, or deforming it (the renaming wasn't designed to be 100% re-applicable).
In case of a few folders, the loop would be over before the stream had the chance to refresh, thus no problems.
so here's how i fixed it. by adding the following method, and iterating through an array of paths instead of the stream.
private Path[] getVerifiedPaths()
{
ArrayList<Path> verifiedFilePaths= new ArrayList<>();
for(Path filepath : fileList)
verifiedFilePaths.add(filepath);
return verifiedFilePaths.toArray(new Path[0]);
}
Path[] filePaths = getVerifiedPaths();
for(Path filePath : filePaths) { ...rename...}
instead of:
for(Path filepath : fileList){...rename...}
thanks to "JB Nizet" for his suggestion (comment above).

Categories

Resources