So, I'm doing a project and now I have a question, so I would like your help :)
First, I already know how to write and read a .txt file, but I want something more than just x.hasNext().
I want to know how could write, read and modify a .txt file like .ini does. What? Simple (I think):
First, write a file like this:
[client1]
name=Bill Gates
nick=Uncle Bill
number=123456789
[client2]
name=Steve Jobs
nick=Stevie
number=987654321
And so many other parameters, just like above, but when I'm wanting to read a specific one (like name or nick of a certain "client") I can do it with easy (Ok, I know it will not be easy, but I think you understood :D)
So, if you already know what I want to learn, please, teach me :) If you don't, please explain me what you didn't understood :D
Thanks in advance for every helping
The format you describe is for a Windows .ini file, back from Windows 3.x days:
http://en.wikipedia.org/wiki/INI_file
Perhaps the closest thing to a "standard format" in Java is a "properties" file; typically in the format "name=value":
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
If you were to write your own program and invent your own initialization file format, would not use an .ini file. Instead, I would recommend:
1) simple properties file (if possible)
... otherwise ...
2) an XML file (if you need multi-level, structured data)
However, if you want to read and write existing .ini files for an existing application, I would either:
1) write my own .ini parser (it isn't difficult)
... or ...
2) Download and run a library likke ini4j:
http://ini4j.sourceforge.net/
'Hope that helps!
The commons-configuration project from apache supports INI file formats:
HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(file);
Set<String> sectionNames = config.getSections();
SubnodeConfiguration section = config.configurationAt(sectionName);
String value = section.getString(keyName);
Check out the javadocs, it's pretty easy to use.
Related
I'm starting on a new budgeting/allowance project and I'm trying to figure out how to save the data between opening and closing the program. Arrays seem clunky and I don't know how I would save the array data to a file anyway. I've really only worked with text files before so this is new to me.
I'm assuming a database of some sort is what I need but I don't know what I should be looking for.
I know this has to be a simple issue but I honestly don't know where to start; any help is greatly appreciated.
That is entirely for you to decide, there is no one right answer here.
You can save in a text file, e.g. CSV if very simple, otherwise JSON or XML are common choices, or in a binary file, e.g. Java serialized objects, or some embedded database file might do.
It really depends on how complex the data is, how big the file can become, whether you want to be able to edit the file directly in a text editor, and how important load/save performance is to you.
Since it's a new project and you seem fairly new to this, I'd suggest JSON or XML, whichever of the two you are more familiar with. But that's just my opinion.
It's entirely your choice.
Files are categorized by file-extension. So my question is, how to identify the file type even the file extension has been changed.
For example, i have a video file with name myVideo.mp4, i have changed it to myVideo.txt. So if i double-click it, the preferred text editor will open the file, and won't open the exact content. But, if i play myVideo.txt in a video player, the video will be played without any problem.
I was just thinking of developing an application to determine the type of file without checking the file-extension and suggesting the software for opening the file. I would like to develop the application in Java.
One of the best libraries to do this is Apache Tika. It doesn't only read the file's header, it's also capable of performing content analysis to detect the file type. Using Tika is very simple, here's an example of detecting a file's type:
import java.net.URL;
import org.apache.tika.Tika; //Including Tika
public class TestTika {
public static void main(String[] args) {
Tika tika = new Tika();
String fileType = tika.detect(new URL("http://example.com/someFile.jpg"));
System.out.println(fileType);
}
}
Structure, magic numbers, metadata, strings and regular expressions, heuristics and statistical analysis... the tool will only be as good as the database of rules behind it.
Try DROID (Digital Record Object IDentification tool) for identifying file types; Java, Net BSD-licensed. It is a free project of the National Archives UK, unrelated to Android. Source is available on Github and Sourceforge. The DROID documentation is good, there's also a getting started guide from the Digital Preservation Coalition.
See also Darwinsys file and libmagic.
There's a tool called TrID that does what you are after - it current supports 5033 different file types - and can be trained to add new types. On *nix systems, there's also the file command, which does something similar.
well, its like having a database of file-format you want to read without looking for extension in your app. Exactly as Linux does. So whenever you open a file, you need to check file-format database which type it belongs to. Though Not sure how will it work for different file types, but most of files have fixed header format, be it zip, pdf, mpg, avi, png, etc.. so this approach should work
You could try MimeUtil2, but it's quite old and though not up2date. The best way is still the file extension.
But the solution from Adam is not as bad as you think. You could build your platform independent solution using a wrapper around command line calls. I think you will get much better results using this method.
The following code snippet retrieves information about the file type
final File file = new File("file.txt");
System.out.println("File type is: " + new MimetypesFileTypeMap().getContentType(file));
Hopefully, it may help you
Im a novice java-programer, whos trying to create a small java app.
In the program im working on, I want to load the configurations from different ini-files.
The basic idea is, that I would have a library containing all the config files, the parser should read all of them and make configurations named after their filenames.
The parser should be created to work dynamicly, so it can read different types of configs.
example
House.ini
-> type0
-> id name height witdh length price_based_on_dimensions
-> id1 name1 height witdh length price_based_on_dimensions
These data should be saved to a config object named config.house. The tricky part is that a different config file, can have its type = type0 but with a different number of attributtes.
I realise that there is no simply solution to this, but any help and or guides to create a dynamic parser is welcome
I'm not really clear on the output you want to produce, but a Java INI parsing library might be a good place to start. For that, you should use ini4j.
Other then ini4j (which is a really good library) I personally prefer to use xml config files. To me they are easier to use and allow for easier configuration
I hope there do have an operation for this topic,'cause I don't want to loop the file once again,and hope to read the file from the specific location say a line number,and then I will read the file with much more threads than just one.
Any idea?
Thanks first!!
To my knowledge there isn't anything like this in the standard Java API. You could use LineIterator or (even just a basic BufferedReader) to build a custom class that does what you need, like this guy did.
Note that a RandomAccessFile sounds promising but unfortunately for you, the seek() method takes an offset in bytes and not in lines so unless your lines are all always the same length, this wont' work for you.
I deal with very large binary files ( several GB to multiple TB per file ). These files exist in a legacy format and upgrading requires writing a header to the FRONT of the file. I can create a new file and rewrite the data but sometimes this can take a long time. I'm wondering if there is any faster way to accomplish this upgrade. The platform is limited to Linux and I'm willing to use low-level functions (ASM, C, C++) / file system tricks to make this happen. The primimary library is Java and JNI is completely acceptable.
There's no general way to do this natively.
Maybe some file-systems provide some functions to do this (cannot give any hint about this), but your code will then be file-system dependent.
A solution could be that of simulating a file-system: you could store your data on a set of several files, and then provide some functions to open, read and write data as if it was a single file.
Sounds crazy, but you can store the file data in reverse order, if it is possible to change function that reads data from file. In that case you can append data (in reverse order) at the end of the file. It is just a general idea, so I can't recommend anything particular.
The code for reversing of current file can looks like this:
std::string records;
ofstream out;
std::copy( records.rbegin(), records.rend(), std::ostream_iterator<string>(out));
It depends on what you mean by "filesystem tricks". If you're willing to get down-and-dirty with the filesystem's on-disk format, and the size of the header you want to add is a multiple of the filesystem block size, then you could write a program to directly manipulate the filesystem's on-disk structures (with the filesystem unmounted).
This enterprise is about as hairy as it sounds though - it'd likely only be worth it if you had hundreds of these giant files to process.
I would just use the standard Linux tools to do it.
Writting another application to do it seems like it would be sub-optimal.
cat headerFile oldFile > tmpFile && mv tmpFile oldFile
I know this is an old question, but I hope this helps someone in the future. Similar to simulating a filesystem, you could simply use a named pipe:
mkfifo /path/to/file_to_be_read
{ echo "HEADER"; cat /path/to/source_file; } > /path/to/file_to_be_read
Then, you run your legacy program against /path/to/file_to_be_read, and the input would be:
HEADER
contents of /path/to/source_file
...
This will work as long as the program reads the file sequentially and doesn't do mmap() or rewind() past the buffer.