The Artima Developer Community
Sponsored Link

Java Answers Forum
reader

1 reply on 1 page. Most recent reply: Jul 13, 2005 2:15 PM by Antonio

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 1 reply on 1 page
Cre'Shannan Thompson

Posts: 1
Nickname: creshannan
Registered: Jul, 2005

reader Posted: Jul 13, 2005 12:16 PM
Reply to this message Reply
Advertisement
Hi

I'm trying to write a program that reads a line for that buffer than open that file. I have already written that program and it open up the file when I enter the directory but it will not open a file when I enter in a line form that file.
can someone please help!!!!

Here is an sample program to gave you an idea of what I'm doing:


import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class TextFile2 extends JFrame implements ActionListener
{

private JTextField enterField;
private JTextArea outputArea;
private JLabel pathLabel;


public TextFile2()
{
super ("OPUS");

enterField = new JTextField("Enter file directory here");

enterField.addActionListener(this);
outputArea = new JTextArea();

ScrollPane scrollPane = new ScrollPane();
scrollPane.add( outputArea );

Container container = getContentPane();
container.add(enterField, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);

setSize( 500, 500);
show();
}

public void actionPerformed (ActionEvent actionEvent )
{
File name = new File (actionEvent.getActionCommand() );



if (name.exists() )
{
outputArea.setText(
name.getName() + " exist\n" +
( name.isFile() ?
" is a file\n" : " is not a file\n" ) +
( name.isDirectory() ?
" is a directory\n" : " is not a directory\n" ) +
( name.isAbsolute() ?
" is absolute path\n" : " is not absolute path\n" ) +
"Last modified: " + name.lastModified() +
"\nPath: " + name.length() +
"\nAbsolute path: " + name.getAbsolutePath() +
"\nParent: " + name.getParent() );

if (name.isFile())
{
try
{
BufferedReader input = new BufferedReader(
new FileReader( name ));
StringBuffer buffer = new StringBuffer();
String text;
outputArea.append("\n\n" );

while ( (text = input.readLine()) != null )
buffer.append(text + "\n" );

outputArea.append ( buffer.toString() );
}

catch (IOException ioException )
{
JOptionPane.showMessageDialog( this,
"FILE ERROR",
"FILE ERROR", JOptionPane.ERROR_MESSAGE );
}
}

else if (name.isDirectory() )
{
String directory[] = name.list();

outputArea.append( "\n\nDirectory contents: \n");

for (int i = 0; i < directory.length; i++ )
outputArea.append( directory + "\n");
}
}

else
{
JOptionPane.showMessageDialog( this, actionEvent.getActionCommand() + "Does NOT Exist",
"ERROR", JOptionPane.ERROR_MESSAGE );
}
}

public static void main (String args[] )
{
TextFile2 application = new TextFile2();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}


Antonio

Posts: 33
Nickname: arhak
Registered: Jul, 2005

Re: reader Posted: Jul 13, 2005 2:15 PM
Reply to this message Reply
It works as it is.
What is the problem?
I think I didn't get it.

(next time enclose the java code between the java tags)

Flat View: This topic has 1 reply on 1 page
Topic: Polymorphism vs Methods Signatures Best Fitness Previous Topic   Next Topic Topic: Passing arguments to a single file in netbeans

Sponsored Links



Google
  Web Artima.com   

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