Java to read and write files

2010-07-26  来源:本站原创  分类:Java  人气:146 

/ *
* Simple read / write a text file example
* Here includes three examples, namely,
* 1. To read the file into memory (in this StringBuffer) example
* 2. The content of the text write to file
* 3. Will read out the contents of a file written to another file
* Also shows that if read from the input stream, output stream to write out the content (only text stream)
* Three examples can exist independently, so one needed only look on the line.
* /

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;

public final class AccessTextFile (

/ **
* 1. Demonstrates the flow of the text read in a StringBuffer
* @ Throws IOException
* /
public void readToBuffer (StringBuffer buffer, InputStream is)
throws IOException (
String line; / / used to save the contents of each line read
BufferedReader reader = new BufferedReader (new InputStreamReader (is));
line = reader.readLine (); / / read the first line
while (line! = null) (/ / if the line is empty read the instructions
buffer.append (line); / / will read the contents to the buffer in
buffer.append ("\ n"); / / Add line breaks
line = reader.readLine (); / / Read the next line
)
)

/ **
* 2. StringBuffer contents of the presentation will be read out to the stream
* /
public void writeFromBuffer (StringBuffer buffer, OutputStream os) (
/ / PrintStream can be convenient to use the contents of the output to the output stream
/ / The object of the same usage and System.out
/ / (System.out PrintStream object itself)
PrintStream ps = new PrintStream (os);
ps.print (buffer.toString ());
)

/ **
* 3 *. copy content from the input stream to the input stream
* @ Throws IOException
* /
public void copyStream (InputStream is, OutputStream os) throws IOException (
/ / The reading process can read the comments readToBuffer
String line;
BufferedReader reader = new BufferedReader (new InputStreamReader (is));
PrintWriter writer = new PrintWriter (new OutputStreamWriter (os));
line = reader.readLine ();
while (line! = null) (
writer.println (line);
line = reader.readLine ();
)
writer.flush (); / / final output stream to determine what should have written out
/ / Here because the os does not close the writer is coming from outside the transmission
/ / Since it is not open from here, and not shut down from here
/ / If you close the writer, packaged inside the os also be closed
)

/ **
* 3. Call copyStream (InputStream, OutputStream) method copies a text file
* /
public void copyTextFile (String inFilename, String outFilename)
throws IOException (
/ / First the input / output file generated the corresponding input / output stream
InputStream is = new FileInputStream (inFilename);
OutputStream os = new FileOutputStream (outFilename);
copyStream (is, os); / / copy the content with copyStream
is.close (); / / is in the open here, so needs to close
os.close (); / / os is open here, so needs to close
)

public static void main (String [] args) throws IOException (
int sw = 1; / / three tests of the selector switch
AccessTextFile test = new AccessTextFile ();

switch (sw) (
case 1: / / test time
(
InputStream is = new FileInputStream ("E: \ \ test.txt");
StringBuffer buffer = new StringBuffer ();
test.readToBuffer (buffer, is);
System.out.println (buffer); / / will read the contents of the write buffer
is.close ();
break;
)
case 2: / / test write
(
StringBuffer buffer = new StringBuffer ("Only a test \ n");
test.writeFromBuffer (buffer, System.out);
break;
)
case 3: / / test copy
(
test.copyTextFile ("E: \ \ test.txt", "E: \ \ r.txt");
)
break;
)
)

)

相关文章
  • [Excellent] recast the hero of the Blade (Broken Blade of Heroes): java read csv (comma delimited) files 2010-06-21

    Recasting the hero of the Blade Bind on Equip Swords 39 - 74 Damage Speed 2.20 + 5 - 10 Fire Damage (29.1 damage per second) +8 Stamina +3 Strength Durability 90/90 Requires Level 33 Just re-write about what predecessors had written. Wrote a reader C

  • Java batch processing to rename files. 2010-10-13

    package com.cbsj.methods; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; public class FileOperator { / ** * @ Param args * / public static void main (String [] args) { / / TODO Auto-generated method stub / / DoSomethin

  • Java read file and write files 2010-10-20

    package com.manze.stu; import java.io.*; public class IOTest { public static void samples() throws IOException { //1. This is the file line by line reading data BufferedReader in = new BufferedReader(new FileReader("D:\\info.txt" )); String s=&q

  • java implementation of all the files in a directory to monitor 2010-12-07

    Transfer from: http://www.iyouf.info/java-implement-file-monitor.html On the network to monitor the file status using java examples are too few articles. There is only going through the motions only. Today, I deliberately put this code inside the sep

  • java how to read. properties files 2011-10-10

    CSDN the study concluded: First, read the introduction. Properties files of six methods 1. Java.util.Properties class to use the load () method example: InputStream in = new BufferedInputStream (new FileInputStream (name)); Properties p = new Propert

  • JAVA operating word reading template files 2010-03-23

    Want to do a java action word of the program, to achieve reading the template file, modify the part, generate a new file such a function. package zb; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOExc

  • opencsv java import csv and xls files summary 2010-04-13

    If the binary file into csv or xls file format needed jxl.jar package to resolve, If the file is comma delimited csv format text file is needed opencsv.jar package to resolve. How to distinguish a binary file format, a simple way to use Notepad to op

  • Java to read and write files 2010-07-26

    / * * Simple read / write a text file example * Here includes three examples, namely, * 1. To read the file into memory (in this StringBuffer) example * 2. The content of the text write to file * 3. Will read out the contents of a file written to ano

  • java data synchronization processing txt files 2010-09-08

    txt file java data synchronization processing requirements: txt file the first line is the last line is the end begin begin end of each line of data with the "|" separate txt file about 30 million lines, 20M about the size of the treatment: I de

  • JAVA How to force delete files? 2011-04-18

    / ** * Try to delete given file, try 10 times * @ Param f * @ Return true if file deleted success, nor false; * / public static boolean forceDelete (File f) { boolean result = false; int tryCount = 0; while (! result & & tryCount + + <10) { Sys

  • 1 Remove all files under specified directory and subdirectories of the Java program 2010-05-04

    Je on today wandering, I saw that many people share a delete all files under specified directory and subdirectories of the Java program is better, so copy this file over to borrow later. Originally posted address: recursive delete all files under spe

  • java.io.IOException: Too many open files 2010-11-01

    One problem: generating a lot of pictures uploaded picture size to 0 Second, the error log: (1) tomcat log StandardWrapperValve [default]: Servlet.service () for servlet default threw exception java.io.IOException: Too many open files at java.io.File

  • method of reading and writing xml files java 2010-11-07

    In java read xml files under the main methods for 4: DOM, SAX, JDOM, JAXB 1. DOM (Document Object Model) This method is mainly provided by the W3C, it will read all the xml file into memory, then all elements a data tree for quick access to each node

  • Exe files packaged into a Java 2010-12-27

    The first step: ① Specify the JAR or class file transformation. 1. Class files can be packaged into a jar package in advance, and then browse, navigate to the jar package 2. Can directly select the directory that contains all class files ② When selec

  • How will your JAVA files into JAR package 2010-12-31

    Tied to your writing and use of natural JAR package for others is a very happy thing under the following brief: (1) course must have a JAVA source file the source files compiled into CLASS (2) When the JAVA files be in a good note under PACKAGE For e

  • Java code to delete files and directories 2011-08-08

    import java.io.File; / ** * @ Date 2011-2-6 * @ Author Javaweb.cc * / public class DeleteFileUtil { / ** * * Delete file, can be a single file or folder * * @ Param fileName * File name to be deleted * @ Return the file deleted successful return true

  • java read file 2009-02-27

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to und

  • MANIFEST.MF *** JAR files in the redeployment of resources 2009-07-18

    Java to open the JAR file, we often can see the file contains a META-INF directory, the directory will be a number of documents, of which there must be a MANIFEST.MF, this document describes many of the Jar file information, the following detailed to

  • java path solution 2009-11-02

    Java path issue, relatively speaking, more complicated. Recent work related to create and read documents, now the actual problems encountered in the use of summarized as follows: The interpretation of a relative path 1. Relative path (that is, vis-à-

  • java implementation file download 2010-03-29

    1, using RequestDispatcher way 1, web.xml file to add <mime-mapping> <extension> doc </ extension> <mime-type> application / vnd.ms-word </ mime-type> </ mime-mapping> 2, the program is as follows: " % @ page langu