Java IO学习笔记+代码(3)
短信预约 -IT技能 免费直播动态提醒
字符流处理
package study.iostudy;
import java.io.*;
public class ProcesserCharacterStream
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
String lineStr;
FileInputStream fileInStream;
InputStreamReader inputReader;
BufferedReader bufReader;
FileOutputStream fileOutStream;
OutputStreamWriter outputWriter;
BufferedWriter bufWriter;
fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
inputReader = new InputStreamReader(fileInStream);
bufReader = new BufferedReader(inputReader);
System.out.println("------------------------------------------------");
System.out.println("There are file content before modify:");
while ((lineStr = bufReader.readLine()) != null)
System.out.println(lineStr);
bufReader.close();
inputReader.close();
fileInStream.close();
fileOutStream = new FileOutputStream("d:\\mydir\\secondFile.txt");
outputWriter = new OutputStreamWriter(fileOutStream);
bufWriter = new BufferedWriter(outputWriter);
String newStr = new String("Modify the file ! \r\nThis is a nice thing. \r\nWe can write anything.");
bufWriter.write(newStr, 0, newStr.length());
System.out.println(newStr);
bufWriter.close();
outputWriter.close();
fileOutStream.close();
fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
inputReader = new InputStreamReader(fileInStream);
bufReader = new BufferedReader(inputReader);
System.out.println("------------------------------------------------");
System.out.println("There are file content after modify:");
while ((lineStr = bufReader.readLine()) != null)
System.out.println(lineStr);
bufReader.close();
inputReader.close();
fileInStream.close();
}
}
接收键盘输入数据
package study.iostudy;
import java.io.*;
public class OutputKeyPress
{
public static void main(String[] args)
{
System.out.println("This is a example about acceptance of keyboard.");
String tempStr = "0";
try
{
InputStreamReader inputReader;
BufferedReader bufReader;
inputReader = new InputStreamReader(System.in);
bufReader = new BufferedReader(inputReader);
tempStr = bufReader.readLine();
System.out.println("Input num is: " + tempStr);
}catch(IOException e)
{
e.printStackTrace();
}
int n = Integer.parseInt(tempStr);
int nultiNum = 1;
for (int i =1; i <= n; i++)
{
nultiNum *= i;
}
System.out.println("multiply of input number is: " + nultiNum);
}
}
过滤流
package study.iostudy;
import java.io.*;
public class FilterStream
{
public static void main(String[] args)
{
try
{
FileInputStream inStream;
FileOutputStream outStream;
BufferedInputStream bufInObj;
BufferedOutputStream bufOutObj;
DataInputStream dataInObj;
PushbackInputStream pushObj;
byte[] tempBuf = new byte[1024];
int copyLen;
inStream = new FileInputStream("d:\\mydir\\secondFile.txt");
outStream = new FileOutputStream("d:\\mydir\\thirdFile.txt");
bufInObj = new BufferedInputStream(inStream);
bufOutObj = new BufferedOutputStream(outStream);
dataInObj = new DataInputStream(inStream);
System.out.println(dataInObj.readBoolean());
while ((copyLen = bufInObj.read(tempBuf, 0, 1024)) != -1)
{
String copyStr = new String(tempBuf);
System.out.println(copyStr);
bufOutObj.write(tempBuf, 0, copyLen);
bufOutObj.flush();
}
int pushData;
byte[] pushByte = {'o', 'k'};
pushObj = new PushbackInputStream(
new FileInputStream("d:\\mydir\\thirdFile.txt"), 1000);
while ((pushData = pushObj.read()) != -1)
{
if (Character.isLetter((char)pushData))
{
System.out.print((char)pushData);
}
else
{
System.out.println();
pushObj.unread(pushByte);
}
}
}catch(FileNotFoundException e)
{
System.out.println("File not found or persission denied.");
}catch(IOException e)
{
System.out.println("error:" + e);
}
}
}
顺序输入流
package study.iostudy;
import java.io.*;
public class SequenceStream
{
public static void main(String[] args)
{
FileInputStream fileStream1, fileStream2;
try
{
String allStr;
fileStream1 = new FileInputStream("d:\\mydir\\secondFile.txt");
fileStream2 = new FileInputStream("d:\\mydir\\thirdFile.txt");
SequenceInputStream seqStream = new SequenceInputStream(
fileStream1, fileStream2);
BufferedInputStream bufObj = new BufferedInputStream(seqStream);
byte[] bufByte = new byte[1024];
while (bufObj.read(bufByte, 0, 1024) != -1)
{
String tempStr = new String(bufByte);
System.out.println(tempStr);
}
}catch(FileNotFoundException e)
{
System.out.println("File not found or no permission.");
}catch(IOException e)
{
System.out.println("error:" + e);
}
}
}
对象串行化
package study.iostudy;
import java.io.*;
class Book implements Serializable
{
String isbn;
String name;
int page;
String type;
public Book(String isbn, String name, int page, String type)
{
this.isbn = isbn;
this.name = name;
this.page = page;
this.type = type;
}
}
public class SerializableObject implements Serializable
{
public static void main(String[] args)
throws IOException, ClassNotFoundException
{
Book bookObj = new Book("7-02-016450-1", "Java", 218, "programming");
FileOutputStream fileOStream = new FileOutputStream("temp.ser");
ObjectOutputStream objOutStream = new ObjectOutputStream(fileOStream);
try
{
objOutStream.writeObject(bookObj);
objOutStream.close();
}catch(IOException e)
{
e.printStackTrace();
}
bookObj = null;
FileInputStream fileInStream = new FileInputStream("temp.ser");
ObjectInputStream objInStream = new ObjectInputStream(fileInStream);
try
{
bookObj = (Book)objInStream.readObject();
objInStream.close();
}catch(IOException e)
{
e.printStackTrace();
}
System.out.println("------------------------------------------------");
System.out.println("There are information about book:");
System.out.println("ISBN Number: " + bookObj.isbn);
System.out.println("Book Name: " + bookObj.name);
System.out.println("Book Page: " + bookObj.page);
System.out.println("Book Type: " + bookObj.type);
}
}
package study.iostudy;
import java.io.*;
public class ProcesserCharacterStream
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
String lineStr;
FileInputStream fileInStream;
InputStreamReader inputReader;
BufferedReader bufReader;
FileOutputStream fileOutStream;
OutputStreamWriter outputWriter;
BufferedWriter bufWriter;
fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
inputReader = new InputStreamReader(fileInStream);
bufReader = new BufferedReader(inputReader);
System.out.println("------------------------------------------------");
System.out.println("There are file content before modify:");
while ((lineStr = bufReader.readLine()) != null)
System.out.println(lineStr);
bufReader.close();
inputReader.close();
fileInStream.close();
fileOutStream = new FileOutputStream("d:\\mydir\\secondFile.txt");
outputWriter = new OutputStreamWriter(fileOutStream);
bufWriter = new BufferedWriter(outputWriter);
String newStr = new String("Modify the file ! \r\nThis is a nice thing. \r\nWe can write anything.");
bufWriter.write(newStr, 0, newStr.length());
System.out.println(newStr);
bufWriter.close();
outputWriter.close();
fileOutStream.close();
fileInStream = new FileInputStream("d:\\mydir\\secondFile.txt");
inputReader = new InputStreamReader(fileInStream);
bufReader = new BufferedReader(inputReader);
System.out.println("------------------------------------------------");
System.out.println("There are file content after modify:");
while ((lineStr = bufReader.readLine()) != null)
System.out.println(lineStr);
bufReader.close();
inputReader.close();
fileInStream.close();
}
}
接收键盘输入数据
package study.iostudy;
import java.io.*;
public class OutputKeyPress
{
public static void main(String[] args)
{
System.out.println("This is a example about acceptance of keyboard.");
String tempStr = "0";
try
{
InputStreamReader inputReader;
BufferedReader bufReader;
inputReader = new InputStreamReader(System.in);
bufReader = new BufferedReader(inputReader);
tempStr = bufReader.readLine();
System.out.println("Input num is: " + tempStr);
}catch(IOException e)
{
e.printStackTrace();
}
int n = Integer.parseInt(tempStr);
int nultiNum = 1;
for (int i =1; i <= n; i++)
{
nultiNum *= i;
}
System.out.println("multiply of input number is: " + nultiNum);
}
}
过滤流
package study.iostudy;
import java.io.*;
public class FilterStream
{
public static void main(String[] args)
{
try
{
FileInputStream inStream;
FileOutputStream outStream;
BufferedInputStream bufInObj;
BufferedOutputStream bufOutObj;
DataInputStream dataInObj;
PushbackInputStream pushObj;
byte[] tempBuf = new byte[1024];
int copyLen;
inStream = new FileInputStream("d:\\mydir\\secondFile.txt");
outStream = new FileOutputStream("d:\\mydir\\thirdFile.txt");
bufInObj = new BufferedInputStream(inStream);
bufOutObj = new BufferedOutputStream(outStream);
dataInObj = new DataInputStream(inStream);
System.out.println(dataInObj.readBoolean());
while ((copyLen = bufInObj.read(tempBuf, 0, 1024)) != -1)
{
String copyStr = new String(tempBuf);
System.out.println(copyStr);
bufOutObj.write(tempBuf, 0, copyLen);
bufOutObj.flush();
}
int pushData;
byte[] pushByte = {'o', 'k'};
pushObj = new PushbackInputStream(
new FileInputStream("d:\\mydir\\thirdFile.txt"), 1000);
while ((pushData = pushObj.read()) != -1)
{
if (Character.isLetter((char)pushData))
{
System.out.print((char)pushData);
}
else
{
System.out.println();
pushObj.unread(pushByte);
}
}
}catch(FileNotFoundException e)
{
System.out.println("File not found or persission denied.");
}catch(IOException e)
{
System.out.println("error:" + e);
}
}
}
顺序输入流
package study.iostudy;
import java.io.*;
public class SequenceStream
{
public static void main(String[] args)
{
FileInputStream fileStream1, fileStream2;
try
{
String allStr;
fileStream1 = new FileInputStream("d:\\mydir\\secondFile.txt");
fileStream2 = new FileInputStream("d:\\mydir\\thirdFile.txt");
SequenceInputStream seqStream = new SequenceInputStream(
fileStream1, fileStream2);
BufferedInputStream bufObj = new BufferedInputStream(seqStream);
byte[] bufByte = new byte[1024];
while (bufObj.read(bufByte, 0, 1024) != -1)
{
String tempStr = new String(bufByte);
System.out.println(tempStr);
}
}catch(FileNotFoundException e)
{
System.out.println("File not found or no permission.");
}catch(IOException e)
{
System.out.println("error:" + e);
}
}
}
对象串行化
package study.iostudy;
import java.io.*;
class Book implements Serializable
{
String isbn;
String name;
int page;
String type;
public Book(String isbn, String name, int page, String type)
{
this.isbn = isbn;
this.name = name;
this.page = page;
this.type = type;
}
}
public class SerializableObject implements Serializable
{
public static void main(String[] args)
throws IOException, ClassNotFoundException
{
Book bookObj = new Book("7-02-016450-1", "Java", 218, "programming");
FileOutputStream fileOStream = new FileOutputStream("temp.ser");
ObjectOutputStream objOutStream = new ObjectOutputStream(fileOStream);
try
{
objOutStream.writeObject(bookObj);
objOutStream.close();
}catch(IOException e)
{
e.printStackTrace();
}
bookObj = null;
FileInputStream fileInStream = new FileInputStream("temp.ser");
ObjectInputStream objInStream = new ObjectInputStream(fileInStream);
try
{
bookObj = (Book)objInStream.readObject();
objInStream.close();
}catch(IOException e)
{
e.printStackTrace();
}
System.out.println("------------------------------------------------");
System.out.println("There are information about book:");
System.out.println("ISBN Number: " + bookObj.isbn);
System.out.println("Book Name: " + bookObj.name);
System.out.println("Book Page: " + bookObj.page);
System.out.println("Book Type: " + bookObj.type);
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341