Java: Marking/Flagging a file - java

I would like to know whether or not there is some way of marking a file to identify whether or not the file contains x.
Consider the following example:
During a batch conversion process I am creating a log file which lists the success / failure of individual conversions.
So the process is as follows:
start conversion process
create log file named batch_XXX_yyyy_mm_dd.log
try to convert 'a'
write success to log file
try to convert 'b'
write success to log file
...
try to convert 'z'
write success to log file
close and persist log file
What I would like to be able to do is mark a file in some way that identifies whether any of the conversions logged in the file were unsuccessful.
I do not want to change the file name (visibly) and I do not want to open the file to check for a marker.
Does anyone have any ideas on how this could be achieved?

You can add file attributes in Java 7 through the java.nio.file.Files class.
So it would be possible to mark whether a file contains X using the Files.setAttribute() method:
Files.setAttribute( "path/to/file", "containsX", true );
And then check whether the file does contain X using the Files.getAttribute( ) method:
Files.getAttribute( "path/to/file", "containsX" )

If you are looking into say
file.log
create another file which will maintain this info say
file.log.status
Your status file can then contain all the information you need. It will be easier to get the status of conversion for all the files as well as easy to map back to original file given a status file.

Related

Java: `A` Archive attribute missing while creating zip programmatically

We are dealing with the decompression libraries/utility that uses attribute to check for the presence of directories/files within the zip.
Problem is that we are not able to set archive bit for a zip while creation. When we create zip programmatically, it wash out previous attributes as well.
We will try to set archive bit with below mentioned steps but not getting desired result so far:
1. Parse each zip entry and getExtra byte[].
2. Use Int value=32 and perform bitwise 'OR' operation.
3. setExtra byte[] after 'OR' operation.
Adding some more details:
We tried following approaches but still this issue is unresolved.
Using setAttribute() method on File system but getting the attributes are getting reset while creating zip.
Files.setAttribute(file, “dos:archive”, true)
Using File.copy() which copies the file attributes associated with the file to the target file but no success. Even existing attributes are not being retained to target file.
Files.copy(path, path, StandardCopyOption.COPY_ATTRIBUTES)
Using ZipEntry.setExtra(byte[]).
found some info online that the java doesn’t have any direct method to set attributes but as per some online articles we found that the extra field is used to set the file permissions on unix and MS DOS file attributes. This is an undocumented field and we didn’t find any reliable information online. Basically, initial 2 bytes are used for unix and last 2 bytes are used for DOS file attributes. We tried setting DOS file attributes with different values in it.
ZipEntry.setExtra(byte[]) - Sets the optional extra field data for the entry.
Using winzip command line tool but not an elegant solution.
I assume it is DOS (Windows)
With Java 7
import java.nio.file.Files;
import java.nio.file.Path;
File theFile = new File("yourfile.zip");
Path file = theFile.toPath();
Files.setAttribute(file, "dos:archive", true);
see: http://kodejava.org/how-do-i-set-the-value-of-file-attributes/

Java - when there is no need of writing to file - should I use input or output stream?

I'm getting from the client an inputStream and file Metadata, and saving it in my SQL table. This table also holds full file path and some unique uid.
I want to be able to pass a uid and get a "handler" to the file, but can't seem to understand if I need to return outputStream, InputStream or File?
Which one should be returned?
I want this handler for the client for the following reasons:
The user will pass it to another function
The user will decide to convert stream to a file and copy it to some local path
Also, When returning outputstram,is it enough to do the following:
OutputStream out = new FileOutputStream(PATH_TO_MY_FILE))
return out;
Am I returning an empty stream? Does out contain all file data?
I thought maybe the best way will be to return file:
File f = new File(PATH_TO_MY_FILE);
return f;
Editing:
My metadata holds file name and file type. When I get InputStream I save in in my folder and set the path in the SQL table to be : folerPath+"/"+filename + "."+ fileType
When The user will run the following function : get(fileUid) I want to retrieve the full path (by using sql query) and return the file (hanlder)
Can you please advise?
Thanks
The user will decide to convert stream to a file and copy it to some local path
This tells us that what you need to give them is an InputStream (or Reader), since they'll be reading from it.
Your code will be reading from your database or whatever, presumably via the InputStream you get back from ResultSet#getBinaryStream or similar. You might give that directly to the caller, or you may prefer to have your code in the middle, perhaps working through a memory buffer.
Re your comment below:
I'm saving the file at some DB folder...
Databases don't have folders; file systems have folders. It sounds like the file isn't stored in your database table, just the path to it. If so, use FileInputStream with the path to get an InputStream for it, which you can return to the caller.

Google Drive Java Api - update file in the folder.

i have a folder in the GoogleDrive. Its named 'lgc'. In it, i have a file info. I know the ID of the folder, but i dont know the ID of 'info' text file in it. I want to UPDATE the content of 'info' file, Conditions to update:
I dont want to change file name or title, only wanting to change its contents.
I dont want to use File ID for 'info' file. Using search Query, that will only result file with name as 'info'.
So, How do i find file without id and How do i Update it? I looked Google Developers website. but please help me, i didnt understood it.
tell me answer to following too: how can i do following?
Before Uploading a file, Check if any file with the same Title is present in Drive.
If present, delete present file and upload new file (Instead of Updating it)
Everything in Drive is keyed from the file ID, which is why you can end up multiple files sharing the same name. To modify content in Drive, you will have to at some stage work with the file ID, you cannot avoid that.
You can search for a file by name (title = 'info' and [parentID] in parents), but you will still need that query to return the file ID so that you can then update.
So your steps would be:
1) files.list where q is: title = 'info' and [parentID for lgc] in parents
2) If you get results, do a files.delete for each file ID returned (you could get more than 1)
3) files.insert for your new info file.
Some notes:
Drive doesn't support what you are trying to do as an atomic operation, which means there are some edge cases you will need to handle if you have a) the potential for multiple applications operate on the same account or b) multiple users operating on the same folder or c) the potential for a user themselves to create a file called 'info' in that lgc folder.
Step 1 may return more than a single file, so you should cater for that in step 2.
Another 'info' file may be created between Step 1 and Step 3, so after step 3 you should run the files.list search again to confirm that only your new file exists.

Apache-Camel console input

I am trying to make a small program that takes in console input such as a user's name, school and other information and then creates a file whose file name is that of the user. Each file will then be located in a folder named after the school. I am not sure how to create a file with those qualities since Camel seems to determine the path and file name before any input is read. Is there a way of getting around this problem?
There is an example on the file component page like so:
// set the output filename using java code logic, notice that this is done by setting
// a special header property of the out exchange
exchange.getOut().setHeader(Exchange.FILE_NAME, "report.txt");
you could replace report.txt with the filename you wish to use.
As for the directory, can you not store the directory name in a header and reference it from the endpoint:
.to("file://${headers.directory}");
more info here: http://camel.apache.org/file2.html

jface.preference.FileFieldEditor can't specify a new file

I'm setting up a series of preferences in my Eclipse (3.5.2) application and I'm having a problem with the FileFieldEditor. I want to allow the user to specify a log file to print output to. Often, this will be a new file. But when I use the file select dialog with FileFieldEditor, it complains that the file doesn't exists ("Value must be an existing file"). Is there a way, without extending the FileFieldEditor class, to suppress this error and have Java create that file if it doesn't exist? Thanks!
When I look the source code of org.eclipse.jface.preference.FileFieldEditor, the only solution would be to extend it and write your own version of a FileFieldEditor, with:
an overwritten changePressed() method in order to keep the file path even if the file does not exists
an overwritten checkState() method in order to avoid that error message.
So I do not see a way to avoid that FileFieldEditor extension here.

Categories