Working with files in Java

Create a file

public void createFile(String filename) {
File file = new File(filename);
// check if file exists
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
}
}
// get path to file
System.out.println("Path: " + file.getAbsolutePath());
}

Read file character by character

public void readFile(String filename) {
File f = new File(filename);
FileInputStream finp;
try {
// open file
finp = new FileInputStream(f);
// read byte by byte
byte b;
do {
b = (byte) finp.read();
System.out.println((char) b);
} while (b!=-1);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

Read file line by line

public void readFile(String filename) {
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println("line:" + line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Wrap stream in another stream

public void readFile(String filename) {
try {
// get all input streams
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
// read file line by line
String line;
while ((line = br.readLine()) != null) {
System.out.println("line:" + line);
}
// close the input stream
dis.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

Yet another example

public void readFile(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
while (bis.available() > 0) {
System.out.println((char) bis.read());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

Writing data to file

public void writeFile(String filename) {
File f = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(f);
if (f.exists()) {
String s = "this is text";
fos.write(s.getBytes());
fos.flush();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

Writing to file

Write data to file. Set the append flag to append data to file (if false overwrite file).

public void appendText(String filename) {
boolean append = true;
try {
FileWriter fw = new FileWriter(filename, append);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("this is line of text");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Get file size

public void getFileSize(String filename) {
File f = new File(filename);
if (f.exists()) {
System.out.println("File is " + f.length() + " bytes long.");
}
}

Get current directory

public void getCurrentDirectory() {
System.out.println(System.getProperty("user.dir"));
}

Get list of files in a directory

public File[] getTxtFiles() {

File dir = new File("/PATH/TO/DIRECTORY/WITH/FILES");

File[] files = dir.listFiles(new FileFilter() {

@Override
public boolean accept(File pathname) {
return pathname.isFile() && pathname.getAbsolutePath().endsWith(".txt");
}
});

for (File f : files) {
System.out.println(f.getAbsolutePath());
}

return files;

}

Rename file

public void renameFile(String name, String newName) {
File file = new File(name);
File newFile = new File(newName);
file.renameTo(newFile);
}