Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

problem in rediredtin GUI to IO Streams

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Is this not printing the contents on console?
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no ... instead give error
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error is
in 77 line
getContents(java.io.File) in ReadLine cannot be applied to (java.lang.strin)

 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry!!!
its line 77 error:
getContents(java.io.File) in ReadFile cannot to applied to (java.lang.String)
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code is:
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think using the following code in the actionPerformed method will pinpoint the problem pretty quickly:
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks swastik and Ulf..
it still givin the same no change.. i am tryin, if you get somethin , kindly
let me know
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m gettin same error
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that you have a folder called files under another folder called watri which is under c:?
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When are you getting the exception? When the application starts or when you click on any file name?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

neha gautam wrote:i m gettin same error


Which is what, exactly? You never posted the full stack trace.
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Can you confirm the said file actually exists?
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha,
Kindly start a new thread for a new topic.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

 
neha gautam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks maneesh....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic