| ||||||||||||||
saving output as textfile Posted by RKM (RKM), 30 May 2003 Hellothis message is related to the message that has the tittle (array in descending order) because I still have little problem since I need the positions with the additional one Also l have to save the output (only the values of positions )in text file ,one value in each line ,so I can use it by another program Thank you for your time. Posted by admin (Graham Ellis), 30 May 2003 If you replaceCode:
with Code:
You'll get each of your position numbers one higher. If you replace System.out with an appropriate PrintStream or PrintWriter object, which you will need to construct / open before you try to write to it, you'll get the output to the file rather than the screen. Posted by RKM (RKM), 1 June 2003 i found some difficulties in constructing the files.actually,I am working on a project which consists of two part each part is written in different language ( the sorting program is the part which is written in java) and I want to connect between the two part using the files,so i want to change that code for example intializing the array must be as following int [] in1= function that open saved file which contains the numbers ( one number in a line) and insert them in the array and at the end I must store the output (only the original positions ) in a text file ( one number in a line) so i can use this file in the second part of the project. i hope that you can help me and thanks in advance Posted by admin (Graham Ellis), 1 June 2003 Quote:
I know the feeling Learning how to handle files in Java is not a trivial task ... In order to know about files, you have to know about exceptions In order to know about exceptions, you need to understand inheritance In order to understand inheritance, you need to know about objects .... Why are you using two languages? What is the second language? Most languages can sort (and many, let's be honest, can sort in a much easier piece of code than Java). It would seem much more practical to use one language rather than two ... Posted by RKM (RKM), 2 June 2003 I " have to" use two language ( java and Oracle) and i can not use only one. and I am using Oracle to build a database and to do other tasks. the two part are finished, but the problem is how to connect them and i think using files-as i described in previous message- is the solution even it is so complicated . please, help me as soon as possible. Posted by admin (Graham Ellis), 3 June 2003 OK - I can appreciate that SQL is a different sort of language and that you need to manipulate the data files further once they're written out by your Oracle client.I've extended my earlier example to output to a file rather than to the standard ouput - this includes opening a print stream, catching any errors in the act of doing the open, outputting to the file, and closing the file on completion. I've also added in that extra "1" to each count that you were asking about earlier. Code:
I am getting concerned that this thread / exchange is moving away from the provision of discussions and answers of points that are of concern to newcomers to Java, and is turning more into a "please write my application for me" thread. Quite apart from the time this takes, I don't feel that I would be doing you a good service in the long term if I provided a piece of code that you didn't fully understand, written to meet a requirement that I don't fully understand. So I'll provide a couple more snippets of code that will help you on the input side of your program, but I'm going to leave the integration of them into the final program you need up to you. Here's a piece of code to read a file ... Code:
... you'll need to add exception handling in case the file isn't available, and you'll want to convert the incoming String to an integer. Have a look at java.lang.Integer where there are a number of suitable methods available - getInteger or parseInt may do the trick for you. Here's an example of the use of parseInt from a piece of code I have lying around - it's part of a more complex example that's splitting up (tokenizing) each input line in a file into a number of variables, but it does give you an example of a call. Code:
Posted by RKM (RKM), 5 June 2003 the program works but the only problem iswhen the file consist of less than 10 numbers import java.util.*; import java.io.*; class TextFileReader { private BufferedReader inputFile; private String fileName; private boolean dataLeft; public TextFileReader (String name) //************************************************ // This constructor opens the file given by the name passed in the parameter. //************************************************ { fileName = name; try {inputFile = new BufferedReader (new FileReader(fileName));} catch (Exception error) { System.err.println ("An error occured opening file " + fileName + "!\n"); System.exit(1); } dataLeft = true; } public String readLine () //************************************************ // This method reads and returns one line of the text file. If the file // is empty, an empty string is returned. //************************************************ { String inputLine; try { if ( (inputLine = inputFile.readLine()) == null ) { dataLeft = false; return ""; } else return inputLine; } catch (Exception error) { System.err.println ("An error occurred reading from file " + fileName + "!\n"); System.exit(1); } return ""; } public boolean moreData () //************************************************ // This function returns true if the end of file has not been read. //************************************************ { return dataLeft; } } public class Open { static TextFileReader inFile; public static void main (String [] args) { float[] array = new float[10]; inFile = new TextFileReader("c:\\me.txt"); for(int i =0;i<10;i++) { String next_line= inFile.readLine(); System. out.println(next_line); array[i] = java.lang.Float.parseFloat(next_line);// intializing array of float type } for(int i=0;i<10;i++) System.out.println(array[i]); } } Posted by admin (Graham Ellis), 6 June 2003 If you can tell us what the errors, it'll be easier to offer advise.This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| |||||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |