how to get the file and folder information in java? - java

I'm doing a project that has a module as "File or folder information". It means, I need all open files and open directory details.
E.g.:
If one file "aa.txt" was open by user, and the file is in "bb directory"; the output should be like
aa.txt that file be in open
bb dir be in open
and also, if you change that file, the message appear by that module as aa.txt that file has been modified by this user.

You won't be able to do that in pure java, you'll need to use a custom library (depending on the OS) as file/user/permissions information is hidden form Java to make it portable. You can only check if you can read or write to a file using standard java (you have few more methods as isDirectory, but not much more).

Please have a look at Java's java.io.File API. You will get most of the things there.
For checking if the file has been modified, you can use lastModified() method.
JDK 7 is giving a new The WatchService API and File Change Notification API. You can read about it here.

Related

How to access file and directories in Linux machine using java program

I have assignment to access file system using java program. In that , I want to create file, read file modify file etc (all operation we can do on file and directory as well).
Is there any way we can do using java program / or any api available to do these operations.
Use java.io.File and java.nio.file.Files. They don't care about which OS (Operating System) are you using. Also File wraps files and folders, check the methods File.isDirectory() and File.isFile().
NOTE: use also File.separator to standarize and avoid problems between OS

Changing file extensions

I am doing some algorithmic problems on the website USACO, and for every submission they want us to make two files for input and output to test values. So if the problem was called "test", they would want users to make the files "test.in" and "test.out" which requires them to change their extensions to ".out" and ".in". How do I change their extensions from ".txt" to ".in" or ".out"?
Note: I am using Windows 8
Thank you
To make Java read files in Eclipse
One must make sure that the words typed in the code and in the name of the file match each other.
For example: The file referenced in must exist.
BufferedReader f = new BufferedReader(new FileReader("test.in"));
Also, you have to make the file under the project folder (in Eclipse or manually in Windows Explorer) because that folder is the directory at for that code, and if it is not in the folder the code will not be able to read it. You can also right-click the project to import files into the folder.
My reference: Trial and Error
http://www.coderanch.com/t/439615/java-io/java/Eclipse-won-read-text-file

How to write a Java program to open a specific file extension (.pef)?

I need to write a java program to open a specific file extension (This is to open a .pef file). This java program is being written to open this .pef file and print it. But I don't know how to write a program that can open the .pef file extension by default.
See:
Desktop.open(File) Launches the associated application to open the file.
Desktop.edit(File) Launches the associated editor application and opens a file for editing.
Desktop.print(File) Prints a file with the native desktop printing facility, using the associated application's print command.
This presumes the application has already been associated with the file-type. To create the file association, launch your app. using Java Web Start & declare the file type in the launch file.
This solution is Windows specific
In this post I am assuming that your program is already capable of reading files of type .pef and printing them. If not then please refer to Treebranch's and Himanshu's posts here to figure out how to do so. Next I will assume that your program is to be invoked on the Windows command line as:
program.exe <filename.pef>
To programatically change the association, follow these steps:
Define a file type
ftype peffile="program.exe" "%1"
Remember to use the absolute path of program.exe here.
Associate this file type with .pef
assoc .pef=peffile
I am not familiar with this file type, but I am guessing you are going to have to read the bytes of the file and then go from there. You should be able to use a FileInputStream. Try looking at this tutorial.
You can use FileInputStream to read a file.
FileInputStream fr=new FileInputStream("xyz.pef");
int i=0;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();

Creating a standalone package in java and take some settings from the configuration file

Anyone plz let us know what to do when we have some configuration file which is basically xml.I want to for example give the path to save the image(for my java program) in a folder from some config file (xml in my case).In that case where should the config file be kept.Rt now every thing is converted to jar file when i create a java standalone package.But i want to give some setting from xml file.What to do in that case.How is it possible.This article only provides to create a single jar file for java project but talks nothing about the configuration settings that u can provide from some external source.
Regards
Sagar
I'm not sure I fully understand your question, but if it is where to put the XML file with configuration information, you can place your xml file in the same directory as your jar file, and then pass the XML file name and path into the Jar on the command line when calling the Jar. If you're running it in Windows, this is often done using a shortcut. Then you can get the full path string for the Jar from the main method's String[] arg array that accepts the command parameters.
Sagar,
The fact your java program is a standalone package (.jar file) has no bearing on where your configuration file is stored. Your java package is a program and that program can read any file from the file system that it so desires; it does not have to be part of the code inside the IDE i.e. you don't have to write it when you write the program. What you do need is some way, when you start the program, to find and read said configuration file.
Depending on how you expect the program to be configured, you might put that file in a number of locations. For example, /etc/yourimageprogram/config.xml or c:\program files\yourimageprogram\config.xml or perhaps c:\users\Sagar\Application Settings\yourimageprogram\config.xml. Which you choose of those options really depends on what the use case is and that I can't help with.
However, there are some main points to reading any file:
Does it exist?
Are we allowed to open it for reading?
Are we allowed to open it for writing? Might want to know if we want to update the config?
In Java, typically, you would test this with:
File configfile = new File("C:\test.xml");
if ( configfile.exists() && configfile.canRead() )
{
// read the file
}
else
{
// decide what to do if no config exists.
// might be first run of app.
}
The next stage is to parse the file. There are a number of parsers available for XML including sax and org.w3c.dom. What you need to do is to use these to extract the information you require and store that in a class. Probably a singleton class as you're unlikely to have multiple configuration instances per instance of the program.
I suggest you read about XML Parsers and File Handling under Java. Also look at the File object. See all your options for file io in java. These should give you some indication of how to proceed.

Text File + JNLP

I’m trying to figure out how to include a reference to a external data file (in text form) that I want distributed along with my application via Web Start (JNLP). Sifting through the documentation for the JNLP structure, I see that you can include references to JAR, nativelib, and extensions – however, I don’t see a means to include a text file resource. How can I accomplish this so that Web Start will download the text file from the server and store it locally along with my application?
I don't believe you can do that.
You can, however, put it on your classpath (in a jar) and reference it through getResourceAsStream().
i just checked that jnlp file is also available as jar file also and as exe file also, so can i open these in a java gui and load my xml file

Categories