基础知识系列一

目标

2020年开始,花好长时间想自己的目标,必须有确切的目标,才能抵抗住各自诱惑。坚持初心,做一个有逼格的coder。
写几部分:1、IO块( 文件,音频,excel ) 2、数据库 3、网络(HTTP) 4、树
5、线程 6、servlet 7、排序,查找

计划

填补java各基础知识块。

IO块

java1.0 主要 面向 字节 (InputStream,OutputStream) ,
后续java1.1后 面向字符 新增 Reader , Writer 两个类

1、InputStream 和 OutputStream 关键类

以下载mp3这个目的来驱动。
站在coder角度
读到程序里解析 就用 InputStream
程序解析完,输出就用 OutputStream
应用场景 主要 面向 字节 , 后续java1.1后 面向字符 新增 Reader , Writer 两个类

子类 FileOutputStream

FileOutputStream fs = new FileOutputStream(“c:/soft/189.mp3”);

网站(/) 和windows系统目录 () 两者是相反的,代码以网站为准。

子类方法 read,write
   FileOutputStream fs = new FileOutputStream(path);
           byte[] buffer = new byte[1204];
           int byteread = 0;
           while ((byteread = inStream.read(buffer)) != -1) {
             fs.write(buffer, 0, byteread);
           }

【个人认识1】 类似于容器的概念:
buffer 这个数组 容器
fs 这个 文件 容器
通过一个媒介(字节数组 byte[] buffer )来传递数据
1、InputStream 对象 中方法 把数据 读到 buffer(字节数组)
2、新建 个 FileOutputStream 对象 (指定 数据存储的地址和形式 )
FileOutputStream fs = new FileOutputStream(“c:/soft/189.mp3”);
3、FileOutputStream 对象 write 方法 从 媒介 中 写进 对象中。


public class FileDownloadMp3 {

   public static void main(String[] args) throws IOException {
   	
   	 String path = "*******.mp3" ; 
       //获取URL对象
       URL url = new URL(path);
       //根据URL打开链接
       URLConnection connection = url.openConnection();
        
       //从连接处获取输入流对象
       InputStream inputStream = connection.getInputStream();
        
       System.out.println("链接成功!");
        
       File file = new File("C:/soft");
        
       if(!file.exists()){
           file.mkdirs();
       }
        
       download("C:/soft/189.mp3", inputStream);
      
       
   }
   
   public static void download(String path,InputStream inStream){
   	FileOutputStream fs;
       try {
           fs = new FileOutputStream(path);
           byte[] buffer = new byte[1204];
            
           int byteread = 0;
           
           while ((byteread = inStream.read(buffer)) != -1) {
           //   fs.write(buffer);   这样写也行 
             fs.write(buffer, 0, byteread);
           }
           System.out.println(path + "保存成功!");
            
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
  
}

2、Reader , Writer , File 类

File 类不仅仅 只代表存在的文件或目录, 也可以用它来创建新的目录, 还可以查看 文件的属性(大小,日期,读写 等等 )
主要应用:1、读取 txt文件 2、写数据 进 txt文件

1.1、读 有路径的文件,用File,FileReader , BufferedReader 类。
1.2、读 流 数据,还是需要用 InputStream

BufferedReader input = new BufferedReader(new InputStreamReader(m_conn.openInputStream()));

读文件 例子


public static void main(String[] args) throws IOException {
	
		/*File 类不仅仅 只代表存在的文件或目录, 也可以用它来创建新的目录。
		 *  还可以查看 文件的属性(大小,日期,读写 等等 )  
		 * */		
		String lastStr = "" ;
		BufferedReader buf = null ;
		try {
			
			 buf = 
					 new BufferedReader(new FileReader(new File("c:/soft/testFile.txt")));
			// 保证 文件里面的内容能完整 读出来。  用readLine 方法。
			String result = null ; 
			while(  (result = buf.readLine() ) != null  ){
				lastStr = lastStr+result ;
				System.out.println(lastStr);
			}  
		} finally {
			  buf.close(); 
		}	
	}

写文件 例子

public static void main(String[] args) throws IOException {
	
		/*File 类不仅仅 只代表存在的文件或目录, 也可以用它来创建新的目录。
		 *  还可以查看 文件的属性(大小,日期,读写 等等 )  
		 * */
		File path = new File("c:/soft/testFile.txt");
		String sb = "{11111}" ;
	
/*	 方法1: 用  FileOutputStream  (true 表示 append ), write时 要转为为 字节	
 * FileOutputStream out = new FileOutputStream(path) ;
		out.write(sb.getBytes());
		out.close(); */
	
		/*方法2: 用 BufferedWriter , true 表示 append    */
		BufferedWriter output = new BufferedWriter(new FileWriter(path,true)) ;
		output.write(sb);
		output.close(); 

读写文件的例子,就暂到这,是很简单的DEMO。
【个人认识2】
Writer类的 write方法, Reader类的 read 方法 , File类相当于媒介。
这点和 个人认识1 里面其实是呼应的。

3、读写 Excel

主要是用JXL第三方包(java excel api),比较初级的操作,能覆盖平时大部分的应用。
2个核心对象1个接口:Workbook (工作簿) , Sheet (工作表) , Cell (单元格)
一个excel文件由一个工作簿组成,一个工作簿又由n个工作表组成,每个工作表又由多个单元格组成。对应于Jxl中的结构为:
在这里插入图片描述

单元格(此处指文本单元格,图像及链接和单元格做为一个层次)分为好多种,所以在API的设计中将 Cell作为一个接口而存在,其有多个对象,比如常用的是 Lable 对象,写文件时,单元格里面是文本类型数据,就用此对象,具体可以见代码。

写excel

 try {
			 //  生成  xls 文件, 数据 单独写入。
			 WritableWorkbook work = Workbook.createWorkbook(new FileOutputStream("E:\\address.xls"));
			 WritableSheet re = work.createSheet("test", 0);
      //创建单元格 ,因为是文本类型所用 Label对象    参数说明: /*column,row,具体内容  */
				 Label addressExcel = new Label(0,1,"12");
				 Label addressExcel2 = new Label(1,1,"34");
			     re.addCell(addressExcel);
			     re.addCell(addressExcel2);
     //写数据进 excel,然后关闭文件簿,必须关闭才能成功。
			 work.write();
			 work.close();
			System.out.println("test");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

***读excel****
public static void main(String[] args) throws RowsExceededException, WriteException, BiffException  {
	
		 try {
			 //读EXCEL文件  。
			 Workbook wb = Workbook.getWorkbook(new File("E:\\address.xls"));
			 
			 Sheet st = wb.getSheet(0) ;
			 
			 System.out.println("行:"+ st.getRows());
			 System.out.println("列:"+ st.getColumns());
			 int j  = 0 ;
			 for (int i = 0; i < st.getRows()  ; i++) {
				 // getCell( column , row )  
				
				 System.out.println( st.getCell(j,i).getContents() );
				 j++ ;
				
			}
			 
          
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值