- public static void readFileByBytes(String fileName) {
- // 一般先创建file对象
- FileInputStream fileInput = null;
- try {
- File file = new File(fileName);
- if (!file.exists()) {
- file.createNewFile();
- }
- byte[] buffer = new byte[1024];
- fileInput = new FileInputStream(file);
- int byteread = 0;
- // byteread表示一次读取到buffers中的数量。
- while ((byteread = fileInput.read(buffer)) != -1) {
- System.out.write(buffer, 0, byteread);
- }
- } catch (Exception e) {
- // TODO: handle exception
- } finally {
- try {
- if (fileInput != null) {
- fileInput.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void readFileByChars(String fileName) {
- FileReader reader = null;
- try {
- File file = new File(fileName);
- if (!file.exists()) {
- file.createNewFile();
- }
- reader = new FileReader(file);
- char[] buffer = new char[1024];
- int charread = 0;
- while ((charread = reader.read(buffer)) != -1) {
- System.out.print(buffer);
- }
- } catch (IOException e) {
- // TODO: handle exception
- } finally {
- try {
- if (reader != null) {
- reader.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void readByBufferedReader(String fileName) {
- try {
- File file = new File(fileName);
- // 读取文件,并且以utf-8的形式写出去
- BufferedReader bufread;
- String read;
- bufread = new BufferedReader(new FileReader(file));
- while ((read = bufread.readLine()) != null) {
- System.out.println(read);
- }
- bufread.close();
- } catch (FileNotFoundException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- public void writeByFileOutputStream() {
- FileOutputStream fop = null;
- File file;
- String content = "This is the text content";
- try {
- file = new File("c:/newfile.txt");
- fop = new FileOutputStream(file);
- // if file doesnt exists, then create it
- if (!file.exists()) {
- file.createNewFile();
- }
- // get the content in bytes
- byte[] contentInBytes = content.getBytes();
- fop.write(contentInBytes);
- fop.flush();
- fop.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fop != null) {
- fop.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void writeByFileReader() {
- try {
- String data = " This content will append to the end of the file";
- File file = new File("javaio-appendfile.txt");
- // if file doesnt exists, then create it
- if (!file.exists()) {
- file.createNewFile();
- }
- // true = append file
- FileWriter fileWritter = new FileWriter(file.getName(), true);
- fileWritter.write(data);
- fileWritter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void writeByBufferedReader() {
- try {
- String content = "This is the content to write into file";
- File file = new File("/users/mkyong/filename.txt");
- // if file doesnt exists, then create it
- if (!file.exists()) {
- file.createNewFile();
- }
- FileWriter fw = new FileWriter(file, true);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(content);
- bw.flush();
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }