I was wondering what universal components there are to a file in a typical Windows x64 architecture and which elements of a file are stored in the OS vs the file... I know there is a file path, but does the file contain that path? Does the file keep track of its own length? Do simple .txt files contain more than the data that corresponds to the text they will display?
Further, when I create a File object in java, passing it an absolute file name, is it really just a reference to a file path? How does it interact with the actual file on my machine?
Related
I am creating some files with dynamic code and dynamic content via code deployed in a Unix system. The name of the file changes when it is placed in a windows shared path.
E.G
Suppose a file name was Ämber.xml which is created in unix system where my code is deployed. After the file creation is finished, it is placed in a windows shared path folder. In the windows shared path folder when I look at the file, the content is perfect but the name changes to Ãmber.xml. So the Ä changes to Ã.
Could you please provide me a solution to this? Thank you.
This is ultimately because an UTF-8 filename is interpreted as ISO (or another non-unicode) encoded string.
Probably the filename encoding is not configured correctly on the Unix system (or a buggy driver is used for mounting), so I'd try checking the mount options for the share. Details vary depending on the OS ("Unix" is pretty general).
I am loading my properties file using the class loader as follows.
Properties prop = new Properties();
prop.load(MyClass.class.getResourseAsStream("/Property.properties"));
Now, using this method I am able to read the properties file. I want to write some data to the property file. I don't know the path of the property file. How do I store the data to the property file then ?
Update
I tried the following, but it doesn't give me the correct path:
File propFile = new File("Property.properties");
System.out.println(propFile.getAbsolutePath());
I don't think you can in a generic way that would always work, because your properties file could be bundled inside a jar, etc. You can get the URL via getResource(String) and then do something with that URL, for example if it's a file URL, you could get the file name there.
URL u=MyClass.class.getResource("/Property.properties");
if ("file".equals(u.getProtocol()){
File f=new File(u.toURI());
}
But that wouldn't work in all cases.
I would write the modified value to a file in a well known location, and use the bundled Properties as the default value, that are overriden by the values in the file.
There are two parts to your question.
First, the reading part. You said: "I am loading my properties file ..." using the code you provided. This code treats the file as a "resource" and loads it using the class loader. The class loader mechanism in the Java Runtime comes into picture here. Roughly speaking, this mechanism searches for it in your application's current classpath and makes the input stream associated with first matching resource available to your code. It may be fine in your case, however, you should realize that if there are multiple properties files by the same name in different parts of your classpath (e.g. different JAR files having the same config file), then you may not know which file is being read. The only way to ensure that you are reading the right file from the classpath is to ensure that you have that config file in a unique place in your application's classpath.
This seems to work for you. Reading from a file is easier, than, say writing to a file.
In the second part, you want to write to a file. It's important to note that you should know the exact whereabouts of the file you are writing to. If you rather unknowingly convert it to an output stream, then you might end up trying to write to a stream that you are not allowed to write to. So, here, you should try to find the location (path) of the actual, physical file, for example, on a computer's hard drive. That path is something you should know before you write and run this program. Remember that a file always exists in a folder or a directory.
Your attempt:
File propFile = new File("Property.properties");
System.out.println(propFile.getAbsolutePath());
to find the location or path of the file you want to write to fails because when you do new File("Property.properties");, Java tries to refer to a file (does not do anything yet with the operating system) that resides in the Java Runtime's current working directory. This refers to the location given by System.getProperty("user.dir"). So, your propFile refers to a (hypothetical) file in that folder and the call to getAbsolutePath() prints its path. This is not the file that you want because you are trying to write to a file whose path you don't know. So, you should find the path of the file and then use a BufferedWriter backed by a FileWriter or BufferedOutputStream backed by a FileOutputStream to write to this file. Make sure that the file you want to write to exists and you have permission to write to it.
Essentially get the resource as an OutputStream, then pass it to the store() method of your Properties object.
Details here:
http://www.drdobbs.com/jvm/readwrite-properties-files-in-java/231000005
I have once again had trouble using JWI Wordnet in Android. The only way to access it, is to create a URL or File pointing to the directory where the files it needs are located. http://projects.csail.mit.edu/jwi/api/index.html I have downloaded the files I need, and added the jar into my project. My error is when I try to create the Dictionary object used to access wordnet. I don't point to a valid directory. Which makes sense, but I just don't know how to do so pointing the correct way. Basically, the folder name is "dict", I can place it into either raw or Assets (Although I read something about files in assets needing to be < 1 mb, the files inside the folder are greater than 1 mb) I need to create either a File or URL that points to said folder. How would I go about doing this?
So, you cannot create a file from raw or assets directly, as they are not stored as files, but you can copy/unpack them somewhere else, and make a File object using the copy. Android: how do I create File object from asset file?
I have a file on an SD card that is a .txt file that my GUI needs to read the problem is that the GUI will be used on different computers and both on mac and windows OS. the .txt file is rather unique so wont be found on any other drive.
is there a way to scan every drive except C drive(to speed things up as i know it wont be there) to find the file so i can load it into my file reader?
thanks for any help?
You can use java.io.File.listRoots() to see the available drives.
You can use a recursive call through the entire file system: You can use the following methods of java.io.File:
listRoots() : list the root files (In Windows it's the drives, on Mac OS X and linux it's '/').
listFiles() : list the enclosing files of a directory
To recursively search for file check the following link
Search directories recursively for file in java
I'm trying to load words into an arrayList from a txt file in the assets folder in my android program, and looking at the api, it says that AssetManager.list(String path) will "Return a String array of all the assets at the given path."
Does that mean that if I have a text file, words.txt, and I put in "words.txt" as the parameter for AssetManager.list(String), that it will return a String array of all the words in the txt file?
If not, how would I go about reading a txt file into an array in my android program?
I couldn't find a definition for "asset"
I'm using eclipse.
EDIT:
I'm not just asking what an asset is, I'm also asking about a specific method in the api and how to load strings into an arrayList
Android offers one more directory where you can keep files which also will be included in package. This directory called /assets. The difference between /res and /assets is that Android doesn’t generate IDs for assets content. You need to specify relative path and name for files inside /assets.
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Source
Asset Usage Example
assets is place where you can normal files which you want to read in application as some point whenever needed. You save your files/directory's. And read in your application from input streams but you cannot write.
As far res concerned, they resource files which you can refer directly in your code. like strings, array of strings,images (drawables),style(android specific like css in html) and there is raw folder which stores all files binary and generates id for it.