What is the best way to assign values to variables of various data types from reading a file (while importing as few things as possible and making use of the simplest code)? I need to write a program which can assign values from a .dat file, if that's relevant. If you could also provide some examples and explanations of such, you will be mentally extolled. Thanks.
I suppose you have already figured out how to write to a file, or you plan to manually make the file. The way I would go about doing this is using a BufferedReader.
Here is some code:
BufferedReader in=new BufferedReader(new FileReader("C:\\users\\sample.dat")); //creates BufferedReader to get value
String line1=in.readLine();//uses reader to save value to String line1
in.close();//closes connection to file
value = Integer.parseInt(line1.trim()); //optional, just to parse if the value is an int
What is the ".dat" file? That extension doesn't really mean anything and many programs store many different types of things using that extension in many different formats. If know what is saved, and, more importantly, how it is saved, we could give you a better answer. P.S. A BufferedReader will only work "properly" for text files.
That also does nothing other than read bytes and interpret them as characters (i.e. text files and text files only). If the data is not being saved as simple text written using a filewriter (or fileoutputstream), but rather as a data or object outputstream you won't be able to use that.
Since the OP doesn't feel the need to tell us exactly what the .dat file consists of (he has told us the "data" that may be in it, but not how that is represented in the file) I no longer fell the need to help.
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.