public class Textfile extends Object
Modifier and Type | Method and Description |
---|---|
static ArrayList<String> |
file2Array(File file)
Same as file2Array (File file, String charset) but uses always UTF-8 as character set.
|
static ArrayList<String> |
file2Array(File file,
String charset)
Reads a textfile in a list of strings.
This method reads line after line (without the newlines at the end) and stores each line as a field in the list. |
static String |
file2String(File file)
Same as file2String (File file, String charset) but uses always UTF-8 as character set.
|
static String |
file2String(File file,
String charset)
Read a (text-)file in to a string.
|
static void |
fileCopy(File source,
File destination)
This method copies the file "source" to the file "destination".
|
static String |
fillWithZero(String s,
int length)
Fills a string with zeros from the beginning if the string is to short.
Examples: Textfile.fillWithZero("23", 1) => "23", Textfile.fillWithZero("23", 2) => "23", Textfile.fillWithZero("23", 3) => "023" |
static InputStream |
getResourceStream(String pkgName,
String fileName)
Get a resource (in particular a file packed into the jar file) and return the content as InputStream.
|
static String |
getTimestampSimple(boolean time)
Get a simple timestamp with or without time.
It uses a Gregorian Calendar the current time in the default time zone with the default locale see java.util.GregorianCalendar constructor GregorianCalendar(). |
static String |
stream2String(InputStream is)
Reads an InputStream and returns a string.
|
static void |
string2File(File file,
String str)
Same as string2File(File file, String str, String charset) but uses always UTF-8 as character set.
|
static void |
string2File(File file,
String str,
String charset)
Stores a string to a text file.
|
public static ArrayList<String> file2Array(File file, String charset) throws IOException
file
- the file to read lines fromcharset
- the character set which was used for the text file, i.e. "UTF-8", "UTF-16" and so on, see java.nio.charset.CharsetIOException
- if it can't read the filehappens if you read UTF-8 with BOM encoded files: http://stackoverflow.com/questions/4897876/reading-utf-8-bom-marker
,
Commons IO: http://commons.apache.org/io/api-release/org/apache/commons/io/input/BOMInputStream.html
public static ArrayList<String> file2Array(File file) throws IOException
file
- the file to read lines fromIOException
- if it can't read the filepublic static String file2String(File file, String charset) throws IOException
file
- the file to read text fromcharset
- the character set which was used for the text file, i.e. "UTF-8", "UTF-16" and so on, see java.nio.charset.CharsetIOException
- if it can't read the filepublic static String file2String(File file) throws IOException
file
- the file to read text fromIOException
- if it can't read the filepublic static void string2File(File file, String str, String charset) throws IOException
file
- the file where to store the stringstr
- string to be written in the filecharset
- the character set which shall be used to encode the string, i.e. "UTF-8", "UTF-16" and so on, see java.nio.charset.CharsetIOException
- if it can't write the filepublic static void string2File(File file, String str) throws IOException
file
- the file where to store the stringstr
- String to be written in the fileIOException
- if it can't write the filepublic static void fileCopy(File source, File destination) throws IOException
source
- source file (often named "in")destination
- destination file (often named "out")IOException
Krüger, Thomas Stark: Handbuch der Java-Programmierung, 6. Auflage, HTML-Ausgabe 6.0.0, Listing 19.4: Kopieren einer Datei
public static String getTimestampSimple(boolean time)
time
- true = append time; false = use date onlypublic static String fillWithZero(String s, int length)
s
- string to insert leading zeros if to shortlength
- length of returned stringpublic static String stream2String(InputStream is)
is
- InputStreampublic static InputStream getResourceStream(String pkgName, String fileName)
pkgName
- name of the package where the resource is, i.e. de.mgmechanics.myflipflops.File.TextfilefileName
- name of the file, i.e. help.txtKrüger, Thomas Stark: "Das Handbuch der Java Programmierung", Addison Wesley Verlag Munich, 6th edition, page 320 ff.
Copyright © 2013. All Rights Reserved.