Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-18-2007, 07:46 AM
Member
 
Join Date: Jun 2007
Posts: 1
aNNuur is on a distinguished road
count character in text file as input file
Hello,

I am a newbie... can anyone help me to figure out this code?

I want to count a character in the input text file and if they are more than 30 character..they will return a error message.

but my problem is.. it fuction only for the first line of the text file and pop the message. the go to the next line and pop the message.

how to make it function : read all the line in the input text file, if more than 30 char in that text file they will pop a message?

import java.io.*;
import javax.swing.JOptionPane;


public class Main {


static public String getContents(File aFile)throws FileNotFoundException, IOException {


if (aFile == null ) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}

StringBuffer contents = new StringBuffer();


BufferedReader input = null;

try {

input = new BufferedReader( new FileReader(aFile) );
String line = null;
(( line = input.readLine()) != null){

long numChar = 0;
numChar += line.length();

JOptionPane.showMessageDialog(null,numChar,"Charac ter in the Message",JOptionPane.INFORMATION_MESSAGE);


contents.append(line);

}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {

input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
return contents.toString();
}


public static void main (String[] args) throws IOException {

File testFile = new File("key1.txt");

JOptionPane.showMessageDialog(null, getContents(testFile),"Original File",JOptionPane.INFORMATION_MESSAGE);

}
}


hmmm? or there is other suggestion?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call the input value from different file? xiongling Networking 6 04-07-2008 11:05 AM
How to get the count of all the lines in a file Java Tip java.io 0 04-06-2008 08:45 PM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM
Finding the character set of a FILE javaplus Advanced Java 1 01-22-2008 07:36 AM
Converting text file(.txt) to JPG file(.jpg) in java javadeveloper Advanced Java 0 11-09-2007 05:22 PM


All times are GMT +3. The time now is 11:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org