The Artima Developer Community
Sponsored Link

Java Answers Forum
A simple question

22 replies on 2 pages. Most recent reply: Sep 19, 2003 9:04 AM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 22 replies on 2 pages [ 1 2 | » ]
John

Posts: 13
Nickname: icej
Registered: Sep, 2003

A simple question Posted: Sep 11, 2003 10:33 AM
Reply to this message Reply
Advertisement
Hi
I have been trying to create a file using visual age java. can someone pls tell me how to save in a file information that is in an array and then how to open it. And pls, can you please show me how to do that but in the simplest form not like other websites that shows a lot of code which i don't understand.
thank you


FredrikN

Posts: 22
Nickname: fredrikn
Registered: Jul, 2003

Re: A simple question Posted: Sep 11, 2003 11:58 AM
Reply to this message Reply
The problem is simple, take a look at this page
http://java.sun.com/docs/books/tutorial/essential/io/index.html

and then write back if you don't understand it

John

Posts: 13
Nickname: icej
Registered: Sep, 2003

Re: A simple question Posted: Sep 11, 2003 1:10 PM
Reply to this message Reply
thank you for the website but i still have a question. I have been trying to get the 'copy' program to work but everytime i run it, it says that it can't find the farrago.txt file. where should that file be at for the program to work? and how do i specify where do i want to save the file and name the file?
pls help me
thank you

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 11, 2003 4:31 PM
Reply to this message Reply
Give the absolute path rather than the relative path in the program. Eg: c:\test\xyz.txt

John

Posts: 13
Nickname: icej
Registered: Sep, 2003

Re: A simple question Posted: Sep 11, 2003 8:33 PM
Reply to this message Reply
hi again.
i still can't get it to work. how do i specify the exact location? where do i modify it?
this is the code that shows up on the web
import java.io.*;

public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1)
out.write(c);

in.close();
out.close();
}
}

i tried to change this part
File inputFile = new File("farrago.txt"); to
File inputFile = new File("c:\farrago.txt"); but it still doesn't work. I don't know what i have to do to make it work.
Also, does it make a difference which windows i'm using? i'm using windows XP.
thank you

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 11, 2003 8:42 PM
Reply to this message Reply
File inputFile = new File("c:\farrago.txt");
File outputFile = new File("c:\outagain.txt");


Hope you are using a Windows OS!

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 11, 2003 8:51 PM
Reply to this message Reply
Sorry I made my post without reading your complete post. The program you have written tries to read from farrago.txt and while reading the in.read() will return how many char are read from the file and it is being written to the out file again.

However the problem is I think there is no input file by the name farrago.txt existing in the system. Why don't you create a text with some contents file called farrago.txt in C:\ and then run the program. And don't forget the change the path to C:\farrago.txt and that will make sure you will get a output file.

However from your original post I understand that you are actually trying to achieve something else? Is that right?

John

Posts: 13
Nickname: icej
Registered: Sep, 2003

Re: A simple question Posted: Sep 11, 2003 9:22 PM
Reply to this message Reply
i tried to do what u said but it gives me this error message: An error occurred while saving copy.Copy: An error occured while parsing source code due to invalid escape sequence. i believe is the part:
File outputFile = new File("c:\outagain.txt");
I also tried to save the file farrago.txt in C:\ but it still doesn't work.
Well, i'm trying to learn how to create a file so that i can save information of for example: information in an array. i would like to learn how to save that in a file and how to open it. The problem is that i go to java.sun.com and it comes out with such big coding that i don't understand. So, can you please show me in the simplest form how to do that?
thank you

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 11, 2003 10:18 PM
Reply to this message Reply
Use double slash instead of single slash.

C:\\farrago.txt

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 11, 2003 10:20 PM
Reply to this message Reply
Also if you don't want big examples here are some small ones.

Goto

http://www.javaalmanac.com/egs/java.io/pkg.html

and check under the heading Reading and Writing . Check the other stuff too, you can learn something out of them.

John

Posts: 13
Nickname: icej
Registered: Sep, 2003

Re: A simple question Posted: Sep 12, 2003 8:52 AM
Reply to this message Reply
i still don't understand one part and can't get it to work
the code that is given in the web is:
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while (str != null) {
System.out.print("> prompt ");
str = in.readLine();
process(str);
}
} catch (IOException e) {
}

what do i put where it says process so that i can read specific words in a text file.
My main question is: for example: if I have:
Int[] valu = new Int[15];
valu[0]= 150;
valu[1]=1000;
etc
how do i pass this information to a file and then when i run the program again, how can i get the info back?
thank you

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: A simple question Posted: Sep 12, 2003 7:48 PM
Reply to this message Reply
Well, you didn't specify the problem very clearly and you didn't explain what things you can understand and what things you can't.

Java file I/O is, for some inexplicable reason, unnecessarily complicated (or maybe there is an explanation; I wonder if Bill has asked any of to Java creators in an interview on Artima about this...). For some reason they didn't make such a common thing simple. Oh well. You can either memorize the various classes you have to use to read and write a file (first learn the necssary APIs, then practice writing a few programs without looking them up), or make yourself a little set of utilities that package and hide all the complexity.

Do you want the output file to be textual? You could use properties if that's the case. If that isn't important, it is pretty easy to just serialize an array of ints, like so:
import java.util.*;
import java.io.*;
 
class ReadinAndWritin
{
   public static void main(String args[]) throws Exception
   {
      int [] fibs = new int[15];
 
      // Let's fill up the array with some interesting and valuable data:
      fibs[0] = 0;
      fibs[1] = 1;
      for(int i = 2; i < fibs.length; i++ )
         fibs[i] = fibs[i-1] + fibs[i-2];
 
      String filename = "c:\\temp\\ReadinAndWritin.serialized";
 
      // Write to file:
      ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
      out.writeObject(fibs);
      out.close();
 
      // Read it in:
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
      int [] stuff = (int[])in.readObject();
      in.close();
 
      // Test that it was read in correctly:
      for(int i=0; i < stuff.length; i++)
      {
         if( stuff[i] != fibs[i] )
            System.out.println( "Error at item " + i + ", should be " + fibs[i] + ", but is " + stuff[i] );
         else
            System.out.println( "Fibonacci of " + i + " was correctly read.   It is " + stuff[i] );
      }
   }
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 12, 2003 7:55 PM
Reply to this message Reply
import java.io.*;
class Test{
	public static void main(String args[]){
		int[] valu = {150,1000,170,180,276,884,876};
		DataOutputStream out = null;
		DataInputStream in = null;
		try {
		    out = new DataOutputStream(new FileOutputStream("C:\\outfile.txt"));
		    for (int i=0; i<valu.length; i++)
		    	out.writeInt(valu[i]);
		} catch (IOException e) {
		}finally{
			try{
					out.close();
				}catch(IOException e){
				}
		}
	    try {
	        in = new DataInputStream(new FileInputStream("C:\\outfile.txt"));
	        String str;
	        while ( in.available() >0 ) {
	            System.out.println(in.readInt());
	        }
 
	    } catch (IOException e) {
	    }finally{
			try{
				in.close();
			}catch(IOException e){
			}
		}
	}
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: A simple question Posted: Sep 12, 2003 7:56 PM
Reply to this message Reply
hay matt seems like you too are online now and checkign the forums :-)

richard

Posts: 3
Nickname: lenard
Registered: Sep, 2003

Re: A simple question Posted: Sep 13, 2003 3:37 AM
Reply to this message Reply
can someone help me to make a java program about scientific calculator and the funtions are the 0-9,+ - * /,clear,sin,cos,tan,the octal and binary conversions thanx.i need it before this month ends.

Flat View: This topic has 22 replies on 2 pages [ 1  2 | » ]
Topic: jini: how much harm does the scsl? Previous Topic   Next Topic Topic: mouse clicked

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use