2013年10月11日星期五

java IO file operations portal classic example - learn these examples, you will find that IO is so simple

IO is JAVASE very important one , is the perfect embodiment of object-oriented , in-depth study IO, you will be able to experience a lot of object-oriented thinking .
did not live in the company dry , review a bit IO, found that many have forgotten, so badly written, just enough for beginners to use . I put my review process to write code stickers, we learn together , and please enlighten enlighten ha. Incidentally discuss IO
1, file copy

try {
File inputFile = new File(args[0]);
if (!inputFile.exists()) {
System.out.println("源文件不存在,程序终止");
System.exit(1);
}
File outputFile = new File(args[1]);
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);

byte date[] = new byte[1024];
int temp = 0;
while ((temp = in.read(date)) != -1) {
out.write(date);
}

in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


2, java read documents: a directory to achieve statistical letters that appear in each file number , number of digits , spaces, and the number of rows, there is no alternative other characters

 String fileName = "D:/date.java.bak";
// String fileName = "D:/test.qqq";
String line;
int i = 0, j = 0, f = 0, k = 0;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine();
while (line != null) {
// System.out.println(line);
char c[] = line.toCharArray();
for (int i1 = 0; i1 < c.length; i1++) {
// 如果是字母
if (Character.isLetter(c[i1]))
i++;
// 如果是数字
else if (Character.isDigit(c[i1]))
j++;
// 是空格
else if (Character.isWhitespace(c[i1]))
f++;
}
line = in.readLine();
k++;
}
in.close();
System.out
.println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k);
} catch (IOException e) {
e.printStackTrace();
}

3, from the file (d: \ test.txt) was found in the string "aa" number of occurrences

try {
BufferedReader br = new BufferedReader(new FileReader(
"D:\\test.txt"));
StringBuilder sb = new StringBuilder();
while (true) {
String str = br.readLine();
if (str == null)
break;
sb.append(str);
}
Pattern p = Pattern.compile("aa");
Matcher m = p.matcher(sb);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("\"aa\"一共出现了" + count + "次");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

4, three ways to read the file

 try {
// 方法一
BufferedReader br = new BufferedReader(new FileReader(new File(
"D:\\1.xls")));
// StringBuilder bd = new StringBuilder();
StringBuffer bd = new StringBuffer();
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
System.out.println(str);
bd.append(str);
}

br.close();
// System.out.println(bd.toString());

// 方法二
InputStream is = new FileInputStream(new File("d:\\1.xls"));
byte b[] = new byte[Integer.parseInt(new File("d:\\1.xls").length()
+ "")];
is.read(b);
System.out.write(b);
System.out.println();
is.close();

// 方法三
Reader r = new FileReader(new File("d:\\1.xls"));
char c[] = new char[(int) new File("d:\\1.xls").length()];
r.read(c);
String str = new String(c);
System.out.print(str);
r.close();

} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

5, three ways to write file

 try {
PrintWriter pw = new PrintWriter(new FileWriter("d:\\1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
"d:\\1.txt")));
OutputStream os = new FileOutputStream(new File("d:\\1.txt"));
// 1
os.write("ffff".getBytes());
// 2
// bw.write("ddddddddddddddddddddddddd");
// 3
// pw.print("你好sssssssssssss");

bw.close();
pw.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

6, reads the file , and to read each line into an array of type double

try {
BufferedReader br = new BufferedReader(new FileReader(new File(
"d:\\2.txt")));
StringBuffer sb = new StringBuffer();
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
sb.append(str + "、");
}

String str = sb.toString();
String s[] = str.split("、");
double d[] = new double[s.length];
for (int i = 0; i < s.length; i++) {
d[i] = Double.parseDouble(s[i]);
}
for (int i = 0; i < d.length; i++) {
System.out.println(d[i]);
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

------ Solution ------------------------------------- -------
give a little advice , in fact, still very simple IO streams , as long as we imagine the stream and the target file into pipes and buckets easily understood and programmed !
not know whether you have seen those top masters say IO This chapter of the video , giving the image a very deep !
------ Solution ---------------------------------------- ----
I was a beginner , I feel quite right for me .
LZ, xiexie .....
------ Solution ---------------------------- ----------------
import java.io. *;

public class CopyBytes {
public static void main (String [] args) throw IOException {
File inputFile = new File ("farrago.txt");
File outputFile = new File ("outagainb.txt");


FileInputStream in = new FileInputStream (inutFile)
FileOututStream out = new FileOutputStream (outputFile);

int c;
while ((c = in.read ())! = -1)
out.write (c);
in.close ();
out.close ();
}
}
This is my copy of the book explains the landlord files copied code established under the byte date [] = new byte [1024]
and date do in.read () parameters do
------ Solution --------------------- -----------------------
good support it.

But IO best in finally close oh
------ Solution ----------------- ---------------------------
try {
File inputFile = new File (args [0]);
if (! inputFile.exists ()) {
System.out.println (" Source file does not exist , the program termination " ) ;
System.exit (1);
}
File outputFile = new File (args [1]);
InputStream in = new FileInputStream (inputFile);
OutputStream out = new FileOutputStream (outputFile);

byte date [] = new byte [1024];
int temp = 0;
while ((temp = in.read (date))! = -1) {
out.write (date);
}

in.close ();
out.close ();
} catch (FileNotFoundException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}




------ Solution ------------------------------------ --------

EXCEL code read as follows:
import java.io. *;
import jxl. *;

public class exercise
{
public static void main (String args [])
{
try
{
Workbook book = Workbook.getWorkbook (new File ("D : \ \ 1.xls "));

Sheet sheet = book.getSheet (0);

Cell cell1 = sheet.getCell (0, 0);
String result = cell1.getContents ();
System.out.println (result);
book.close ();
}
catch (Exception e)
{
System.out.println (e);
}
}
}

- ----- For reference only ---------------------------------------
for example, there are many relatively junior delete these files are created , I do not stick out, we believe that these are no problem. I posted mainly for file read and write operations
------ For reference only --------------------------- ------------
I was a beginner , I feel quite right for me .
LZ, xiexie .....
------ For reference only -------------------------- -------------
  This reply was moderator deleted at 2011-05-11 18:00:23

------ For reference only ---------------------------------- -----
I am also new to the ah thank you ah
------ For reference only ------------------ ---------------------
looked a long time , I did not understand

------ For reference only ---------------------------------- -----
will be a !
------ For reference only -------------------------------------- -
landlord , recently I'm confused here. . . Have to say . . . You came just in time

Dingdingding
------ For reference only ------------------------------- --------
" the perfect embodiment of the object-oriented "

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:25:13

------ For reference only ---------------------------------- -----
I was a beginner , I feel quite right for me .
LZ, xiexie .....
------ For reference only -------------------------- -------------
  This reply was moderator deleted at 2011-05-12 08:36:46

------ For reference only ---------------------------------- -----
not review what really forget !
------ For reference only ---------------------- -----------------


is indeed the case . It is important programming ideas, I found it a really IO is the perfect embodiment of the object-oriented , abstract classes and interfaces , polymorphism, various design patterns reflected on the inside of the head. Careful studies is good.
------ For reference only -------------------------------------- -
my column was incomplete, a lot of things I do not Tieshanglai . For example, to write the file , there are several ways .
Today, only the realization of functional literacy , and the difference between the specific variety of handy, you do it slowly . Some things can only sense it , -
------ For reference only --------------------------- ------------
Oh programming is cattle
------ For reference only --------------- ------------------------
  This reply was moderator deleted at 2011-05-12 08:36:46

------ For reference only ---------------------------------- -----
make the whole more useless

IO stream to figure out two , byte and character streams

then understand decorative pattern on the end. . .


------ For reference only ---------------------------------- -----
Thank you to share
------ For reference only ---------------------------- -----------
useful summary of benefit , thanks prawns .
------ For reference only -------------------------------------- -
also
------ For reference only -------------------------------- -------
Thank you to share
------ For reference only -------------------------- -------------
  This reply was moderator deleted at 2011-05-12 09:28:02

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:28:01

------ For reference only ---------------------------------- -----
landlord did not make it clear, just out of the program . . . .
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2011-05-12 09:41:01

------ For reference only ---------------------------------- -----
Thank landlord .
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2011-05-12 09:42:22

------ For reference only ---------------------------------- -----
LZ able to read large files you write such a TXT file parsing 60M above . facie landlord method ?
------ For reference only -------------------------------------- -
Thank you to share , and see nothing wrong !
------ For reference only -------------------------------------- -

my whole are the primary use. Just look at the API just write something, it is certainly not specific . Otherwise you are tidy , let me learn everyone
------ For reference only --------------------------- ------------
useful ah , thank you
------ For reference only ------------------- --------------------
  This reply was moderator deleted at 2011-05-12 09:38:21

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:43:06

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:43:06

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:43:05

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:43:35

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 09:43:34

------ For reference only ---------------------------------- -----
Recommended landlord spirit !
------ For reference only -------------------------------------- -
newcomers, thank landlord recommended !
------ For reference only -------------------------------------- -
this post must be collections
------ For reference only ---------------------------- -----------
good ah ! Thank you !
------ For reference only -------------------------------------- -
posts per day back to get 10 points that can be available points ! yeah
------ For reference only ----------------------------------- ----
feel quite right for me .

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 10:40:34

------ For reference only ---------------------------------- -----
first reply then look
------ For reference only ------------------------- --------------
  This reply was moderator deleted at 2011-05-12 10:41:02

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 10:41:27

------ For reference only ---------------------------------- -----
thank you for sharing , very useful
------ For reference only ----------------------- ----------------
thank you for sharing , very useful
------ For reference only ------------ ---------------------------

c = in.read (), you this is one byte a word section to read, then a byte by byte write .
byte date [] = new byte [1024], this is the first 1024 bytes of space to open up , and then one reads the byte into the date in 1024 , and then once the data is written to date inside.
You say, what efficiency ?
------ For reference only -------------------------------------- -

you're quite right, in development are generally in the finally close the .
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2011-05-12 11:49:02

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 13:43:05

------ For reference only ---------------------------------- -----
collection , the future will be used ! LZ thanks !
------ For reference only -------------------------------------- -
bother !
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2011-05-12 13:43:03

------ For reference only ---------------------------------- -----
landlord mighty dragon powerful
------ For reference only ------------------------ ---------------
Thank LZ a ~
------ For reference only ---------------- -----------------------
  This reply was moderator deleted at 2011-05-12 14:06:37

------ For reference only ---------------------------------- -----
IO simply looking through the book , a routine !
------ For reference only -------------------------------------- -

Excel file you can actually read the draw , you're too strong ! ! ! !
I would like to know if you read something out of the horse is God ! ! ! !
estimated that only God can recognize a horse piles astronomical character . . . . .


------ For reference only ---------------------------------------
good, thank you ! !
------ For reference only -------------------------------------- -

I love this ecstasy ~
------ For reference only ------------------------ ---------------

escel there is something, read out is something. Proposed multi -on practice exercises
------ For reference only -------------------------------- -------

excel, engage engage it
------ For reference only ------------------ ---------------------
  This reply was moderator deleted at 2011-05-12 14:55:14

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 14:55:14

------ For reference only ---------------------------------- -----
  This reply was moderator deleted at 2011-05-12 14:50:53

------ For reference only ---------------------------------- -----
just need to thank you sharing
------ For reference only ---------------------- -----------------
Thank you to share !
------ For reference only --------------- ------------------------
xls landlord should also be used to read some of the external package it
------ For reference only ---------------------------------------
collection / / content is too short ?
------ For reference only ------------------------------------- -
Thank you, thank
------ For reference only ------------------------------- --------

an empty excel sheet read something as follows :

Mi as??? {00020P819-
Document = ThisWorkbook / & H00000000
Document = Sheet1 / & H00000000
Document = Sheet2 / & H00000000
Document = Sheet3 / & H00000000
Name = "VBAProject"
HelpContextID = "0"
VersionCompatible32 = "393222000"
CMG = "1416F73A79F97DF97DF97DF97D"
DPB = "D7D5347B34FDF6FEF6FEF6"
GC = "9A9879B83DB93DB9C2"

[Host Extender Info]
& H00000001 = {3832D640-CF90-11CF-8E43-00A0C911005A}; VBE; & H00000000

[Workspace]
ThisWorkbook = 0, 0, 0, 0, C
Sheet1 = 0, 0, 0, 0, C
Sheet2 = 0, 0, 0, 0, C
Sheet3 = 0, 0, 0, 0, C
there are many bible characters do
------ For reference only ---------------------------------------
landlord · how are · processing node stream flow is it ? Data stream it ? Object stream it ?
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2011-05-13 11:04:15

------ For reference only ---------------------------------- -----

I posted the only part , looked at yesterday posted today , is a bit chaotic.
Today I tidied , feel more self- improvement .
feel downright had time to study , it now appears is actually very simple . Mainly input and output streams , character byte stream , then the stream of bytes into characters converted , buffered input and output streams , and so on.
clarify the class hierarchy , decorative design pattern clear, all came out. Still very regular.

------ For reference only ---------------------------------- -----
experts to supplement it
------ For reference only -------------------------- -------------
  This reply was moderator deleted at 2011-05-13 09:01:58

------ For reference only ---------------------------------- -----
you tell me something about how to use input and output streams do ? They can print something , I do not know when to use ah , I was inexperienced ,
------ For reference only -------------------- -------------------
  This reply was moderator deleted at 2011-05-13 09:02:43

------ For reference only ---------------------------------- -----
landlord really to force ,
------ For reference only ------------------------ ---------------
  This reply was moderator deleted at 2011-05-13 11:14:26

------ For reference only ---------------------------------- -----
while ((temp = in.read (date)) ! = -1 ) {
out.write (date);
}

in.close ();
out.close ();


read and write streams in here to close the case, is likely to cause internal occupy finished, the server crashes
------ For reference only ----------------- ----------------------
taught , and very practical.
------ For reference only -------------------------------------- -
write so-so
------ For reference only ------------------------------- --------
zan lz
------ For reference only ------------------------ ---------------
hope that the landlord can hang a little more advanced stuff ! Let's sightseeing !
------ For reference only -------------------------------------- -
IO important is what the flow on the line selected
------ For reference only ------------------------ ---------------
] +1
------ For reference only ----------------- ----------------------
thanks for sharing !
------ For reference only -------------------------------------- -
Thank landlord ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
------ For reference only ---------- -----------------------------

------ For reference only ----------------- ----------------------
  This reply was moderator deleted at 2011-05-13 09:46:58

没有评论:

发表评论