Java RandomAccessFile Read readFile(File f)

Here you can find the source of readFile(File f)

Description

read File

License

Open Source License

Declaration

public static List<String> readFile(File f) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static File err = null;
    public static PrintWriter errwriter = null;

    public static List<String> readFile(File f) {
        err = new File(f.getParent() + "/neispravni_inventarni_brojevi.txt");
        List<String> retVal = new ArrayList<String>();
        try {/*from w w w  . j a  v a  2  s. c  om*/
            errwriter = new PrintWriter(err);
            RandomAccessFile in = new RandomAccessFile(f, "r");
            String line;
            while ((line = in.readLine()) != null) {
                String invBr = parseLine(line);
                if (invBr != null)
                    retVal.add(invBr);
                else
                    errwriter.append(line + "\n");
            }
            errwriter.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retVal;
    }

    public static String parseLine(String line) {
        if (line.contains("/")) {
            String odeljenje = line.substring(line.indexOf("/") + 1);
            if (odeljenje.length() != 2)
                return null;
            String broj = line.substring(0, line.indexOf("/"));
            if (broj.length() > 9 || !isNumber(broj))
                return null;
            return odeljenje + "00000000".substring(0, 9 - broj.length())
                    + broj;
        } else {
            return line;
        }
    }

    public static boolean isNumber(String str) {
        for (int i = 0; i < str.length(); i++)
            if (!Character.isDigit(str.charAt(i)))
                return false;
        return true;

    }
}

Related

  1. read_structdesc(final RandomAccessFile raf)
  2. read_tagdesc(RandomAccessFile raf)
  3. read_typedesc(final RandomAccessFile raf)
  4. read_UnsignedByte(RandomAccessFile raf)
  5. readAByte(ReadableByteChannel readChannel)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(final File file)
  9. readFile(String absoluteFileName)