1.利用FileInputStream和FileOutputStream,完成下面的要求:
(1) 用FileOutputStream 在当前目录下创建一个以自己名字命名的文件,如“陆杰.txt”,并向文件输出自己的专业、班级、籍贯等信息,如果文件已存在,则在原有文件内容后面追加。
(2) 用FileInputStream读入该文件,并在控制台上打印出文件中的内容。
(3) 要求用try-catch-finally处理异常,并且关闭流应放在finally块中。
package wxy;
import java.io.*;
public class wxy1 {
private FileOutputStream fos=null;
private FileInputStream dis=null;
public void out(){
try {
File file=new File("E:\\学习\\java\\第六次\\src\\wxy.txt");
fos=new FileOutputStream(file);
byte[] data="2020级软件工程2班 四川".getBytes();
fos.write(data);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void duru(){
File file=new File("E:\\学习\\java\\src\\wxy.txt");
try {
dis=new FileInputStream(file);
byte[] b=new byte[1024];
int length=0;
while ((length=dis.read(b))!=-1){
System.out.println(new String(b,0,length));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
wxy1 Wxy1=new wxy1();
Wxy1.out();
Wxy1.duru();
}
}
2. 利用数据流,完成下面操作:
(1)判断当前目录下是否存在一个“xiaomu.dat”的文件,如果该文件不存在,则往该文件中写入一个long类型的数值:20210512L 。
(2) 如果该文件存在,则从该文件中读出数值,并把该数值加1之后,再存回文件中。
package wxy;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class wxy2
{
private DataInputStream dis=null;
private DataOutputStream dos=null;
private File file=new File ("xiaomu.dat");
private File file2=new File ("xiaomu2.dat");
public void go()
{
try{
if (file .exists())
{
dis = new DataInputStream(new FileInputStream(file));
dos = new DataOutputStream(new FileOutputStream(file2));
long n=dis.readLong();
n++;
dos.writeLong(n);
System.out.println(n);
System.out.println("写出数据n完毕!");
}else
{
dos = new DataOutputStream(new FileOutputStream(file));
long data=20210512L;
dos.writeLong(data);
System.out.println("写出数据data完毕!");
}
dos.flush();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(dis!=null)
{
try{
dis.close();
}catch (IOException e){
e.printStackTrace();
}
}
if(dos!=null)
{
try{
dos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
public void duqu()
{
try{
dis = new DataInputStream(new FileInputStream(file));
dos = new DataOutputStream(new FileOutputStream(file2));
long n=dis.readLong();
n++;
dos.writeLong(n);
System.out.println(n);
System.out.println("写出数据n完毕!");
dos.flush();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(dis!=null)
{
try{
dis.close();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(dos!=null)
{
try{
dos.close();
}catch ( IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void copy()
{
try{
dis = new DataInputStream(new FileInputStream(file2));
dos = new DataOutputStream(new FileOutputStream(file));
long n=dis.readLong();
dos.writeLong(n);
dos.flush();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(dis!=null)
{
try{
dis.close();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(dos!=null)
{
try{
dos.close();
}catch ( IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
wxy2 Wxy2=new wxy2();
//Wxy2.go();
//Wxy2.duqu();
Wxy.copy();
}
}
3.利用BufferedInputStream和BufferedOutputStream拷贝一张图片,从一个目录到另外一个目录下。注意:对图片进行复制,而不是移动。
package wxy;
import java.io.*;
public class wxy3 {
private FileInputStream fis=null;
private FileOutputStream fos=null;
private BufferedInputStream bis=null;
private BufferedOutputStream bos=null;
public void copy(){
File f1=new File("E:\\学习\\java\\第六次\\src\\1.jpg");
File f2=new File("E:\\学习\\java\\第六次\\src\\2.jpg");
try {
fis=new FileInputStream(f1);
fos=new FileOutputStream(f2);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
byte[] b=new byte[1024*6];
int len=bis.read(b);
while (len!=-1){
bos.write(b,0,len);
len = bis.read(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
wxy3 Wxy3=new wxy3();
Wxy3.copy();
}
}
4. 编写一个程序,从键盘输入若干个字符,当输入字符“#”时中止输入。统计输入字符的个数,并将他们按输入时的逆序输出到以自己学号命名的文件中。
package wxy;
import java.io.*;
import java.util.Scanner;
public class wxy4 {
private FileOutputStream fos=null;
private Scanner input=new Scanner(System.in);
private int sum=0;
public String input(){
String s = "";
int i=0;
while (true){
System.out.println("请输入一个字符:(#表示终止输入)");
char c=input.next().charAt(0);
s+=c;
i++;
if(c=='#')
break;
}
this.sum=i;
return s;
}
public void out(String s){
try {
File file=new File("E:\\学习\\java\\第六次\\src\\wxy.txt");
fos=new FileOutputStream(file);
byte[] data=s.getBytes();
fos.write(data);
System.out.println("写入txt文件成功!");
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String reverse(String s){
String newS="";
for (int i = s.length()-1; i >=0; i--)
newS = newS + s.charAt(i);
return newS;
}
public static void main(String[] args) {
wxy4 Wxy4=new wxy4();
String s=Wwxy4.input();
String newS= Wxy4.reverse(s);
System.out.println("字符个数为:"+Wxy4.sum);
Wxy4.out(newS);
}
}
5.使用数据流技术将文本文件“练习.txt”的内容按行读出,每读出一行就顺序添加行号,并写入到以自己姓名班级命名(如,杨木20级1班.txt)的文件中。
package wxy;
import java.io.*;
public class wxy5
{
private BufferedReader bReader=null;
private BufferedWriter bWriter=null;
private File sourceFile=new File("c:\\wxy\\练习.txt");
private File destFile=new File("wxy2班.txt");
private void jiahanghao() {
try {
this.bReader = new BufferedReader(new FileReader(sourceFile));
this.bWriter = new BufferedWriter(new FileWriter(destFile));
String strLine = this.bReader.readLine();
int id = 0;
while (strLine != null) {
id++;
strLine = String.valueOf(id)+" :" + strLine;
this.bWriter.write(strLine);
this.bWriter.newLine();
strLine = this.bReader.readLine();
}
this.bWriter.flush();
System.out.println("写出完毕!");
} catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (this.bReader != null) {
try {
this.bReader.close();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
if (this.bWriter != null) {
try {
this.bWriter.close();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void duqu()
{
try {
this.bReader = new BufferedReader(new FileReader(destFile));
String strLine = this.bReader.readLine();
while (strLine != null)
{
System.out.println(strLine);
strLine = this.bReader.readLine();
}
this.bWriter.flush();
System.out.println("写出完毕!");
} catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (this.bReader != null) {
try {
this.bReader.close();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
wxy5 Wxy5=new wxy5();
Wxy5.jiahanghao();
Wxy5.duqu();
}
}