aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem in rediredtin GUI to IO Streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in rediredtin GUI to IO Streams" Watch "problem in rediredtin GUI to IO Streams" New topic
Author

problem in rediredtin GUI to IO Streams

neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
hello all...... i m a bit new to swings,, but tried writin some codes, i have 3 programs here..

1st one is list all files of directory



2nd is.. to read a file...



3rd is... the creation of GUI..


kindly tell me how do i print the content of the file i have taken from my 2nd program, and also tell give me and idea how can we count the number of times enter has been pressed in a text file, so that i can merge it wid my code..
thanks to all


Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

System.out.println(" "+data);


Is this not printing the contents on console?


Swastik
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
no ... instead give error
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
and i need the display on my window(GUI display) , not the console,
pardon me if i m usin the wrong technical words
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

If its a syntax error look at your code, you are using System.println, it should be System.out.println. Or else let us know what is the error?
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
the error is
in 77 line
getContents(java.io.File) in ReadLine cannot be applied to (java.lang.strin)

neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
sorry!!!
its line 77 error:
getContents(java.io.File) in ReadFile cannot to applied to (java.lang.String)
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
code is:
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

your method expects a File object to be passed but you are passing a String ,

you code


should be




And as far as displaying the contents in gui is concerned you need to use components like JTextArea, JEditorPane etc.

For e.g. if I use a JTextArea the code should be
jTextAraObj.setText(data);
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
thanks for everythin...
but on execution if select a file.. its givin me series of exception..File not found Exception
i included textearea as :




kindly tell me any mistake i have done. thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27
I think using the following code in the actionPerformed method will pinpoint the problem pretty quickly:


Ping & DNS - updated with new look and Ping home screen widget
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
thanks swastik and Ulf..
it still givin the same no change.. i am tryin, if you get somethin , kindly
let me know
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27
My point was that you should examine the full path of the file you're trying to open; that way you'll see what's really going on (as opposed to what you seem to assume is going on).
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

well, there was a small mistake in my last code, try this once

public void actionPerformed(ActionEvent e)
{

file_name_selected= e.getActionCommand();
File f=new File(file_name_selected);
File f1=new File(f.getAbsolutePath());
data=ReadFile.getContents(f1);
System.out.println(data);
ta.setText(data); // i do not know the replacement of this line to print the content of that selected file on the screen

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27

File f=new File(file_name_selected);
File f1=new File(f.getAbsolutePath());

This makes no sense. Both f and f1 will point to the same file.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
thanks all..
my code after addin textarea is :



very sorry if i m botherin you guys muc... please tell me if i did some thin incorrect..
the error still persists , i m still checkin on it
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6



public void actionPerformed(ActionEvent e)
{



file_name_selected= e.getActionCommand();
//mention the complete path here, as you can see in my last post
data=ReadFile.getContents(new File("C:\\watri\\files\\"+file_name_selected));
//JTextArea a=new JTextArea(10,30);
//a.setText(data);
//just see what comes in console later you can display it in textarea
System.out.println(data);

}

neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
i m gettin same error
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

Are you sure that you have a folder called files under another folder called watri which is under c:?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

When are you getting the exception? When the application starts or when you click on any file name?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27
neha gautam wrote:i m gettin same error

Which is what, exactly? You never posted the full stack trace.
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
its the exceptions i get when i click the filename

they are:

C:\watri>java UI_new
file: ElecBallast_02mmApart_38mmOffGND_20dBAtten.xls
file: P1k.txt
file: Rled.txt
file: UI(2).java
java.io.FileNotFoundException: c:\watri\filesP1k.txt (The system cannot find the
file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at ReadFile.getContents(ReadFile.java:18)
at UI_new.actionPerformed(UI_new.java:96)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

java.io.FileNotFoundException: c:\watri\filesP1k.txt (The system cannot find the
file specified)


Can you confirm the said file actually exists?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
ya definitely..... thats why the files it displayed them initially...

this part:
C:\watri>java UI_new
file: ElecBallast_02mmApart_38mmOffGND_20dBAtten.xls
file: P1k.txt
file: Rled.txt
file: UI(2).java
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

You are searching in "C:\\watri\\files", but the error message says "c:\watri\filesP1k.txt" - notice the missing \ between the path and the file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
thanks rob, but i noticed it, but didnt help,
the code i m executin is


kindly tell me if you find any error
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1397
    
    6

First of all is you code compiling?

problem#1 public UI() looks like a constructor, but wrong because your class name is UI_new

problem#2 new File("C:\\watri\\files\\"+file_name_selected), File object is being created , however I did not find any reference to File class in your import statements.
neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
hey guys thanks a lot for everythin, it came on console...
thanks so so much for help...
i jus have to get it in textarea and move further.
thanks again
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Neha,
Kindly start a new thread for a new topic.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

neha gautam
Ranch Hand

Joined: Mar 31, 2009
Posts: 32
thanks maneesh....
 
Don't get me started about those stupid light bulbs.
 
subject: problem in rediredtin GUI to IO Streams
 
Similar Threads
need help with exception
filenotfound Exception encountered,kindly help
help with textarea insertion
how to delete a file
Delete files in a directory