Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class ParentClass{
- public static void main(String[] args) {
- File file = new File("C:\\Java_Scratch\\someFile.txt");
- try {
- Scanner sc = new Scanner(file);
- try {
- Thread.sleep(5000);
- }
- catch(InterruptedException ie) {
- System.out.print(ie.getStackTrace()) ;
- }
- while (sc.hasNextLine()) {
- int i = sc.nextInt();
- System.out.println(i);
- }
- sc.close();
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- try {
- int i = ParentClass.countLines("C:\\Java_Scratch\\someFile.txt");
- }
- catch (IOException ioe) {
- System.out.print("ioe" + ioe.getStackTrace() );
- }
- }
- // putting the count function
- public static int countLines(String filename) throws IOException {
- InputStream is = new BufferedInputStream(new FileInputStream(filename));
- try {
- byte[] c = new byte[1024];
- int count = 0;
- int readChars = 0;
- boolean empty = true;
- while ((readChars = is.read(c)) != -1) {
- empty = false;
- for (int i = 0; i < readChars; ++i) {
- if (c[i] == '\n') {
- ++count;
- }
- }
- }
- return (count == 0 && !empty) ? 1 : count;
- } finally {
- is.close();
- }
- }//END method countLines
- // end-count_func
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement