-
Help me!
I'm a high school junior and I currently have a java class in which the teacher
refuses to help us on an assignment telling us to "Use your resources" so
I see the internet, and forums as 'resources' heres my question.
I need an easy way to read input from a text file.
this is the original programme, and I need a way to make the lines that are
printed pop up in dialogue boxes..
import java.io.*;
class me {
public static void main(String[] args) {
try {
// Open the file
FileWriter outputFile = new FileWriter("output.txt");
PrintWriter output = new PrintWriter(outputFile);
// Using output.print("Doh!Man's life..?") prints all on one line
// but using output.println("...") goes to a new line after printing
output.print("Doh!Man's life, ");
output.println("A scant seventeen years in this world, though I've learned
more in this time then quite a few people have in their whole lives. ");
output.println("Currently I'm typing another bio for java, which brings me
up to a total of two I believe, could be worse though in web design we have
made atleast four bios. \n");
System.out.println("Blah blah blah blah blah.");
System.out.println("Eat poo you bastards!");
// We should CLOSE the file at the very end of our program
output.close();
} catch (IOException i) {
System.err.println("An error occured trying to open the file");
// Find out more about the error
i.printStackTrace();
// We can't continue, so finish the program return;
}
}
}
any help/comments/ideas/improvements on my original code, and on the code
to perform the task would be MUCH appreciated.
Thanks
-
Re: Help me!
Hi,
This code below should answer your query...
The code reads from the file passed as command line argument and displays
text in MessageBox...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MsgApp extends JFrame
{
String fileName;
public String readFromFile()
{
// Reading File
BufferedReader reader=null;
String msg="";
try
{
reader=new BufferedReader(new FileReader(fileName));
String tmp=null;
int lines=0;
while(true)
{
tmp=reader.readLine();
msg=msg+tmp+"\n";
if(tmp==null || lines>5)
break;
lines++;
}
reader.close();
}
catch(Exception e)
{
System.out.println("Error IO "+e);
msg="Error in Reading..";
}
return msg;
}
public app2(String fileName)
{
this.fileName=fileName;
getContentPane().setBackground(Color.red);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String msg;
msg=readFromFile();
JOptionPane.showMessageDialog(null,msg,"HelloWorld",JOptionPane.INFORMATION_MESSAGE);
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
MsgApp win=new MsgApp(args[0]);
win.setSize(300,300);
win.setVisible(true);
}
}
"Doh!man" <onw_co@hotmail.com> wrote:
>
>I'm a high school junior and I currently have a java class in which the
teacher
>refuses to help us on an assignment telling us to "Use your resources" so
>I see the internet, and forums as 'resources' heres my question.
>
>I need an easy way to read input from a text file.
>
>this is the original programme, and I need a way to make the lines that
are
>printed pop up in dialogue boxes..
>
>import java.io.*;
>
>class me {
> public static void main(String[] args) {
> try {
>// Open the file
>FileWriter outputFile = new FileWriter("output.txt");
>PrintWriter output = new PrintWriter(outputFile);
>
>// Using output.print("Doh!Man's life..?") prints all on one line
>// but using output.println("...") goes to a new line after printing
>output.print("Doh!Man's life, ");
>output.println("A scant seventeen years in this world, though I've learned
>more in this time then quite a few people have in their whole lives. ");
>output.println("Currently I'm typing another bio for java, which brings
me
>up to a total of two I believe, could be worse though in web design we have
>made atleast four bios. \n");
>
>System.out.println("Blah blah blah blah blah.");
>System.out.println("Eat poo you bastards!");
>
>// We should CLOSE the file at the very end of our program
>output.close();
>} catch (IOException i) {
>System.err.println("An error occured trying to open the file");
>// Find out more about the error
>i.printStackTrace();
>
>// We can't continue, so finish the program return;
>}
>}
>}
>
>
>any help/comments/ideas/improvements on my original code, and on the code
>to perform the task would be MUCH appreciated.
>Thanks
>
>
>
-
Re: Help me!
it didnt work my friend.. got some goofy errors and the applet wouldnt initialize.
"Star:Applet not initialized." was what my applet told me, so I'm clueless.
Any further help? Much thanks, and honour to your family.
Doh!Man
"Rajkamal Gopinath" <funkamal@yahoo.com> wrote:
>
>Hi,
>
>This code below should answer your query...
>
>The code reads from the file passed as command line argument and displays
>text in MessageBox...
>
>
>import javax.swing.*;
>import java.awt.*;
>import java.awt.event.*;
>
>import java.io.*;
>
>public class MsgApp extends JFrame
>{
>String fileName;
>
> public String readFromFile()
> {
>
> // Reading File
>
> BufferedReader reader=null;
> String msg="";
>
> try
> {
> reader=new BufferedReader(new FileReader(fileName));
>
> String tmp=null;
>
> int lines=0;
>
> while(true)
> {
> tmp=reader.readLine();
> msg=msg+tmp+"\n";
> if(tmp==null || lines>5)
> break;
> lines++;
>
> }
> reader.close();
> }
> catch(Exception e)
> {
> System.out.println("Error IO "+e);
> msg="Error in Reading..";
> }
> return msg;
>
> }
> public app2(String fileName)
> {
>
> this.fileName=fileName;
> getContentPane().setBackground(Color.red);
> addMouseListener(new MouseAdapter()
> {
> public void mousePressed(MouseEvent e)
> {
> String msg;
> msg=readFromFile();
>
> JOptionPane.showMessageDialog(null,msg,"HelloWorld",JOptionPane.INFORMATION_MESSAGE);
>
> }
> });
>
> addWindowListener(new WindowAdapter()
> {
> public void windowClosing(WindowEvent e)
> {
> dispose();
> System.exit(0);
> }
> });
> }
> public static void main(String args[])
> {
> MsgApp win=new MsgApp(args[0]);
> win.setSize(300,300);
> win.setVisible(true);
>
> }
>}
>
>
>"Doh!man" <onw_co@hotmail.com> wrote:
>>
>>I'm a high school junior and I currently have a java class in which the
>teacher
>>refuses to help us on an assignment telling us to "Use your resources"
so
>>I see the internet, and forums as 'resources' heres my question.
>>
>>I need an easy way to read input from a text file.
>>
>>this is the original programme, and I need a way to make the lines that
>are
>>printed pop up in dialogue boxes..
>>
>>import java.io.*;
>>
>>class me {
>> public static void main(String[] args) {
>> try {
>>// Open the file
>>FileWriter outputFile = new FileWriter("output.txt");
>>PrintWriter output = new PrintWriter(outputFile);
>>
>>// Using output.print("Doh!Man's life..?") prints all on one line
>>// but using output.println("...") goes to a new line after printing
>>output.print("Doh!Man's life, ");
>>output.println("A scant seventeen years in this world, though I've learned
>>more in this time then quite a few people have in their whole lives. ");
>>output.println("Currently I'm typing another bio for java, which brings
>me
>>up to a total of two I believe, could be worse though in web design we
have
>>made atleast four bios. \n");
>>
>>System.out.println("Blah blah blah blah blah.");
>>System.out.println("Eat poo you bastards!");
>>
>>// We should CLOSE the file at the very end of our program
>>output.close();
>>} catch (IOException i) {
>>System.err.println("An error occured trying to open the file");
>>// Find out more about the error
>>i.printStackTrace();
>>
>>// We can't continue, so finish the program return;
>>}
>>}
>>}
>>
>>
>>any help/comments/ideas/improvements on my original code, and on the code
>>to perform the task would be MUCH appreciated.
>>Thanks
>>
>>
>>
>
-
Re: Help me!
Hi,
I gave the sample thinking you wanted for an application not an applet..
for applet the code is pretty same but you need to change the policy
file since Applets cannot have direct access to Local File System.. you need
to grant permissions for applets to access your file system.
Use Policy Tool and edit the appropriate permissions...
Regards
Rajkamal
"Doh!Man" <onw_co@hotmail.com> wrote:
>
>it didnt work my friend.. got some goofy errors and the applet wouldnt initialize.
>
>"Star:Applet not initialized." was what my applet told me, so I'm clueless.
>Any further help? Much thanks, and honour to your family.
>
>Doh!Man
>
>"Rajkamal Gopinath" <funkamal@yahoo.com> wrote:
>>
>>Hi,
>>
>>This code below should answer your query...
>>
>>The code reads from the file passed as command line argument and displays
>>text in MessageBox...
>>
>>
>>import javax.swing.*;
>>import java.awt.*;
>>import java.awt.event.*;
>>
>>import java.io.*;
>>
>>public class MsgApp extends JFrame
>>{
>>String fileName;
>>
>> public String readFromFile()
>> {
>>
>> // Reading File
>>
>> BufferedReader reader=null;
>> String msg="";
>>
>> try
>> {
>> reader=new BufferedReader(new FileReader(fileName));
>>
>> String tmp=null;
>>
>> int lines=0;
>>
>> while(true)
>> {
>> tmp=reader.readLine();
>> msg=msg+tmp+"\n";
>> if(tmp==null || lines>5)
>> break;
>> lines++;
>>
>> }
>> reader.close();
>> }
>> catch(Exception e)
>> {
>> System.out.println("Error IO "+e);
>> msg="Error in Reading..";
>> }
>> return msg;
>>
>> }
>> public app2(String fileName)
>> {
>>
>> this.fileName=fileName;
>> getContentPane().setBackground(Color.red);
>> addMouseListener(new MouseAdapter()
>> {
>> public void mousePressed(MouseEvent e)
>> {
>> String msg;
>> msg=readFromFile();
>>
>> JOptionPane.showMessageDialog(null,msg,"HelloWorld",JOptionPane.INFORMATION_MESSAGE);
>>
>> }
>> });
>>
>> addWindowListener(new WindowAdapter()
>> {
>> public void windowClosing(WindowEvent e)
>> {
>> dispose();
>> System.exit(0);
>> }
>> });
>> }
>> public static void main(String args[])
>> {
>> MsgApp win=new MsgApp(args[0]);
>> win.setSize(300,300);
>> win.setVisible(true);
>>
>> }
>>}
>>
>>
>>"Doh!man" <onw_co@hotmail.com> wrote:
>>>
>>>I'm a high school junior and I currently have a java class in which the
>>teacher
>>>refuses to help us on an assignment telling us to "Use your resources"
>so
>>>I see the internet, and forums as 'resources' heres my question.
>>>
>>>I need an easy way to read input from a text file.
>>>
>>>this is the original programme, and I need a way to make the lines that
>>are
>>>printed pop up in dialogue boxes..
>>>
>>>import java.io.*;
>>>
>>>class me {
>>> public static void main(String[] args) {
>>> try {
>>>// Open the file
>>>FileWriter outputFile = new FileWriter("output.txt");
>>>PrintWriter output = new PrintWriter(outputFile);
>>>
>>>// Using output.print("Doh!Man's life..?") prints all on one line
>>>// but using output.println("...") goes to a new line after printing
>>>output.print("Doh!Man's life, ");
>>>output.println("A scant seventeen years in this world, though I've learned
>>>more in this time then quite a few people have in their whole lives. ");
>>>output.println("Currently I'm typing another bio for java, which brings
>>me
>>>up to a total of two I believe, could be worse though in web design we
>have
>>>made atleast four bios. \n");
>>>
>>>System.out.println("Blah blah blah blah blah.");
>>>System.out.println("Eat poo you bastards!");
>>>
>>>// We should CLOSE the file at the very end of our program
>>>output.close();
>>>} catch (IOException i) {
>>>System.err.println("An error occured trying to open the file");
>>>// Find out more about the error
>>>i.printStackTrace();
>>>
>>>// We can't continue, so finish the program return;
>>>}
>>>}
>>>}
>>>
>>>
>>>any help/comments/ideas/improvements on my original code, and on the code
>>>to perform the task would be MUCH appreciated.
>>>Thanks
>>>
>>>
>>>
>>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks