Java IO实战操作(二)

 

/**
	 * 向文件中追加新内容
	 * @throws  IOException
	 */
	public void NewInserNum() throws IOException {
		String fileName = "D:" + File.separator + "IO.txt";
		File file = new File(fileName);
		OutputStream out = new FileOutputStream(file, true);
		String str = "我在成都名流学习SoftWare";
		byte[] Bbyte = str.getBytes();
		for (int i = 0; i < Bbyte.length; i++) {
			out.write(Bbyte[i]);
		}
		out.close();
	}

/**
	 * 获取准确大小空间读取文件
	 * @throws IOException
	 */
	public void SelectFile() throws IOException {
		String fileName = "D:" + File.separator + "IO.txt";
		File file = new File(fileName);
		InputStream in = new FileInputStream(file);
		byte[] Bbyte = new byte[(int) file.length()];
		in.read(Bbyte);
		System.out.println("文件长度为:" + file.length());
		in.close();
		System.out.println(new String(Bbyte));
	}

/**
	 * 一个一个的读 
	 */
	public void OneSelectFile() throws IOException {
		String fileName = "D:" + File.separator + "hello.txt";
		File f = new File(fileName);
		InputStream in = new FileInputStream(f);
		byte[] b = new byte[(int) f.length()];
		for (int i = 0; i < b.length; i++) {
			b[i] = (byte) in.read();
		}
		in.close();
		System.out.println(new String(b));
	}

/**
	 * 不知道文件有多大的情况下读取文件 
	 */
	public void NotSelectFile() throws IOException{
		 String fileName="D:"+File.separator+"hello.txt";
		 File f=new File(fileName);
		 InputStream in=new FileInputStream(f);
		 byte[] b=new byte[1024];
		 int count =0;
		 int temp=0;
		 while((temp=in.read())!=(-1)){
			 b[count++]=(byte)temp;
		}          
		 in.close();
		 System.out.println(new String(b));  
	}

/**
	 * 字符流 
	 * @throws IOException 
	 */
	public void StringFile() throws IOException{
		String fileName="D:"+File.separator+"hello.txt";
		File f=new File(fileName);
		Writer out =new FileWriter(f);
		String str="hello";
		out.write(str); 
		out.close();  
	}
/**
	 * 字符流
	 * 从文件中读出内容 
	 */
	public void OutStringFile() throws IOException{
        String fileName="D:"+File.separator+"hello.txt";
        File f=new File(fileName);
        char[] ch=new char[100];
        Reader read=new FileReader(f);
        int temp=0;
        int count=0;
        while((temp=read.read())!=(-1)){
        	ch[count++]=(char)temp;
        }          
        read.close();
        System.out.println("内容为"+new String(ch,0,count));  
	}

/**
	 * 文件复制 
	 * @throws IOException 
	 */
	public void CoutStringFile(String[]args) throws IOException{
		if(args.length!=2){
			System.out.println("命令行参数输入有误,请检查");
			System.exit(1);
			}          
		File file1=new File(args[0]);
		File file2=new File(args[1]);
		if(!file1.exists()){
			System.out.println("被复制的文件不存在");
			System.exit(1);
			}
		InputStream input=new FileInputStream(file1);
		OutputStream output=new FileOutputStream(file2);
		if((input!=null)&&(output!=null)){
			int temp=0;
			while((temp=input.read())!=(-1)){
				output.write(temp);
				}
			}          
		input.close();
		output.close();   
	}
/**
	 * 将字节输出流转化为字符输出流 
	 * @throws IOException 
	 */
	public void OutputStream() throws IOException{
		String fileName= "d:"+File.separator+"hello.txt";
		File file=new File(fileName);
		Writer out=new OutputStreamWriter(new FileOutputStream(file));
		out.write("hello");
		out.close();
	}
/**
	 * 将字节输入流变为字符输入流 
	 * @throws IOException 
	 */
	public void InputStream() throws IOException{
		String fileName= "d:"+File.separator+"hello.txt";
		File file=new File(fileName);
		Reader read=new InputStreamReader(new FileInputStream(file));
		char[] b=new char[100];
		int len=read.read(b);
		System.out.println(new String(b,0,len));
		read.close();  
	}
	/**
	 * 使用内存操作流将一个大写字母转化为小写字母 
	 * @throws IOException 
	 */
	public void ByteArray() throws IOException{
		String str="ROLLENHOLT";
ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
		ByteArrayOutputStream output=new ByteArrayOutputStream();
		int temp=0;
		while((temp=input.read())!=-1){
			char ch=(char)temp;
			output.write(Character.toLowerCase(ch));
		}
		String outStr=output.toString();
		input.close();
		output.close();
		System.out.println(outStr); 
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值