This question already has answers here:
Modify a hidden file in Java
(2 answers)
Closed 10 years ago.
I have a problem with my hidden files in Java. I would like to write to a hidden txt file but it always says "access denied".
My suggestion would be to make the file visible, write into it and then make it hidden again.
But how can i make a hidden file visible?
I previously said: The problem is not that the file is hidden. The problem is that your program doesn't have write access for the file. Making it "unhidden" is unlikely to help.
It seems that it is more complicated than that for Windows. Certainly it is worth trying to "unhide" the file before modifying it.
Anyway, if you are using Java 7, the way to read and write Windows-specific file attributes is to use the java.nio.file.Files API, and specifically the getFileAttributeView(...) method. For older versions you will either need to use an external utility (see #Achintya Jha's Answer) or use a JNI / JNA wrapper to call a Windows native API.
If you are using Linux, change Windows-specific to POSIX and/or Linux specific. (Note that a lot of this stuff to do with file attributes and permissions is intrinsically OS specific.)
Finally, if the problem is that the program doesn't have write access to the file, there is a fair chance that there is nothing that it can do to get write access.
Related
This question already has answers here:
How to break a file into pieces using Java?
(2 answers)
Closed 11 months ago.
Hello I need to split a zip file into multiple samaller files,
ex. file.zip is splitted into files with fixed size less than 640MB and format nameOfZip.zip.001
I am currently looking for some kind of a java library that would be able to do this. If there is no such, advice would be very helpful too.
Zip, the format, can do this. You can then unzip the batch of files with plain jane unzip on the command line (or with tools like p7zip and such, or you can just doubleclick the zip on a mac, etcetera).
Unfortunately, the baked in zip support in java can't make split zips. But, Lingala / Zip4j can do it.
Add that library to your list of dependencies and use its API (forget about java.io.ZipOutputStream, or the Zip FileSystem - anything that starts with java.* can't do this.
Alternatively it is trivial to write code in java to just split any file. You'd need to write java code as well to put them back together, or you need to know a few things about your OS to do this (e.g. cat a.bin b.bin >c.bin on posix OSes will put a.bin and b.bin back together). This isn't difficult at all, just your basic file and outputstream support can trivially put it together in less than a page's worth of java code. No libraries exist, and probably never will - that is a very simple task that isn't common enough to make a library for.
So, if that's what you're looking for, go ahead and write it yourself. All you need is the javadoc of java.nio.file.Paths and java.nio.file.Files.
This question already has answers here:
How to get a JavaDoc of a method at run time?
(5 answers)
Closed 7 years ago.
I would like to display some text before service class run. This text could be HTML, generated from class' javadoc. Is it possible to access/generate it from class itself?
Now with Java 8 you have a whole interface to play with comments and documentation. Check this package com.sun.source.doctree
You need access to the source code files to be able to read comments. If you don't have access (which is most likely the case if you want to read the comments at runtime) you will have to use another solution, like providing the text as a string or resource file.
It's possible to get comments using the Java Compiler API. See: How to access comments from the java compiler tree api generated ast?. The problem here is you still need to know where the source code files are located on the system, if they are even there at all.
Another solution is to use an annotation processor. It has the advantage that it provides you with an abstract representation of the source code without the need to manually read files. You would need to identify the class you want to read the comments from and then use Element.getDocComment.
This question already has answers here:
How to access specific raw data on disk from java
(8 answers)
Closed 9 years ago.
I have a problem with raw disk access in Windows. I wrote program in Java, which works with USB flash device, reads its MBR and etc. Device is opened as RandomAccessFile.
If i open NetBeans and execute by program as usual user i have exception
Exception in thread "main" java.io.FileNotFoundException: \\.\PhysicalDrive2 (Отказано в доступе)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:241)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:122)
at main.USBFlashDevice.<init>(Main.java:90)
at main.Main.main(Main.java:196)
Java Result: 1
If i do it as admin, everything is OK.
What can i do to execute program and edit MBR when i work as usual user on somebody's PC?
What can i do to execute program and edit MBR when i work as usual user on somebody's PC?
You can't.
Writing to the raw disc device is a potentially dangerous operation. It could effectively trash the file system.
Even raw disk read access allows you to read files that are denied by normal Windows access control.
It is therefore mandatory that any program that accesses raw disk has admin privilege. The operating system insists on this. Note: this applies to all multi-user operating systems, not just Windows.
You have to run the program with admin privilege. An if you are really asking how to do that ... your question is off topic. Try asking on "superuser.com".
But I should also say that:
Writing this kind of stuff is dangerous and probably a bad idea. Use an existing utility; e.g. one provided by Microsoft or a reputable 3rd-party software provider.
If you (really) need to write this yourself, doing it in Java is probably a poor choice. Most people would use C or C++.
I know many people have asked similar question but i want to specifically ask how do we call both a .dll and a header file in one java program. In this program i must use the dll file and include the header file if written in c++. But i am not so good in using c++, i want to use java, so how to i achieve this? I know about jna which is good when calling dll files in java and jni, used when calling c/c++ files in java. But in this case, i need to use the dll file in a java program and the c++ header file must be included in order for the program to perform very well.
This question already has answers here:
What is the $1 in class file names?
(6 answers)
Closed 8 years ago.
After successfully creating some applets, I embedded them in a webpage and discovered that ALL the class files must be included. Leave one out and it won't work.
After several iterations of an Applet, there are several class files:
filename.class
filename$1.class
filename$2.class, etc.
I tried using only the filename.class, tried just the last one, tried the first and last... but, as I said, all the class files must be included for the webpage.
Question(s):
1) The filename$n.class (n=some number) files seem to be created at the whim of Eclipse - is there an explanation of this (I searched w/o success)?
2) Even though the class files are only ~4kb, How do I do a cleanup such as to blow away all the filename$n.class files and still be able to embed in a webpage?
Any recommendations?
Thanks
These classes are created from anonymous classes created in your applet - most likely event listeners and such.
They would be created (maybe with other names) by other compilers, too.
If you really want to avoid them, program without anonymous (and other inner) classes. But this results often in an ugly style, so this is not recommended.
If you don't want to upload all the individual class files to the server (and then the browsers having to fetch them all individually), think about putting them all in one jar file, and referencing this in your applet-tag.