JAVA学习第八天
2015-07-23 19:33
726 查看
使用SAX方法解析
PrintStream setOut
DOM解析
BufferedReader_Test
BufferedWriter_test
zip类型的文件挑选
copyFile
FileInputStream_read
FileOutputStream_write
MySAX_Test
PrintStream setOut
DOM解析
BufferedReader_Test
BufferedWriter_test
zip类型的文件挑选
copyFile
FileInputStream_read
FileOutputStream_write
使用SAX方法解析
MySAXHandlerpackage com.lingzhuo.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MySAXHandler extends DefaultHandler { @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); System.out.println("开始解析文档"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri, localName, qName, attributes);//attributes属性 System.out.println("开始标签"+qName); if(qName.equals("Weather")){ System.out.println("Attributes"+attributes.getValue("name")); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); System.out.println("结束标签"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); System.out.println("标签内容"+new String(ch,start,length)); } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub super.endDocument(); System.out.println("解析文档结束"); } }
MySAX_Test
//使用SAX方法解析 package com.lingzhuo.sax; import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class MySAX_Test { public static void main(String[] args) { // TODO Auto-generated method stub SAXParserFactory factory=SAXParserFactory.newInstance();//创建了个工厂 File f=new File("e://14.txt"); try { SAXParser parser=factory.newSAXParser();//创建了个解析器 MySAXHandler handler=new MySAXHandler();//一个管理者 parser.parse(f,handler);//handler管理者 //解析器的一个算法parse可以解析文件 // parse(File f, DefaultHandler dh) //使用指定的 DefaultHandler 将指定文件的内容解析为 XML。 } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
PrintStream setOut
//���ļ���д���� PrintStream setOut������� package com.lingzhuo.test; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; public class PrintStreamSetOut { public static void main(String[] args) { File file =new File("e://14.txt"); try { PrintStream ps=new PrintStream(file); System.setOut(ps); System.out.println("nihaowoshi"); System.out.println("nihaowoyeshihaoren ooo"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
DOM解析
/// package com.lingzhuo.test; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XML_Test1 { public static void main(String[] args) { File file = new File("E://Weather.txt"); // 1.得到DOM解析器的编码实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();// 得到工厂实例 // DocumentBuilderFactory定义工厂 API,使应用程序能够从 XML文档获取生成 DOM 对象树的解析器。 // newInstance()获取 DocumentBuilderFactory 的新实例。 // /假设把你的文档看成一个单独的对象,DOM就是如何用html或者xml对这个对象进行操作和控制的标准。 try { // 2.从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder();// DOM解析器 // 解析XML文档,得到一个Dodument,即DOM树 Document doc = db.parse(file); // Document parse(File f) // 将给定文件的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。 // 得到所有节点列表信息 NodeList weathers = doc.getElementsByTagName("Weather");//tag标签 // getElementsByTagName按文档顺序返回包含在文档中且具有给定标记名称的所有 Element(元素) 的 NodeList。(节点列表) // System.out.println( weathers.getLength());==1长度为1中有北京的天气 for (int i = 0; i < weathers.getLength(); i++) { Node weather = weathers.item(i);//得到weather这个节点 NamedNodeMap map = weather.getAttributes();//NamedNodeMap 对象表示一个无顺序的节点列表。 //我们可以通过节点名称来访问 NamedNodeMap 中的节点。 System.out.println("1111" + map.getNamedItem("name").getNodeValue()); // getNamedItem(String name) // 检索通过名称指定的节点 // String(为返回类型) getNodeValue() // 此节点的值 for (Node node = weather.getFirstChild(); node != null; node = node.getNextSibling()) { // Node getNextSibling() 直接在此节点之后的节点。 if (node.getNodeType() == Node.ELEMENT_NODE) { //该节点为元素 //getNodeType()表示基础对象的类型的节点 System.out.println(node.getNodeName());//getNodeName()此节点的名称 if (node.getFirstChild() != null) { System.out.println(node.getFirstChild().getNodeValue()); } } } } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数, // 目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。 } }
BufferedReader_Test
package com.donghe.test; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReader_Test { public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("e:22.txt"); FileInputStream in; try { in = new FileInputStream(file);//FileInputStream读字节的8位 InputStreamReader is = new InputStreamReader(in);//InputStreamReader读字符的 字符16位的 字节8位 BufferedReader br = new BufferedReader(is); String s = br.readLine();// 读一行 // System.out.println(s); while (s != null) { // s = br.readLine();//放在这里的话 不会重新读而是会接着读 System.out.println(s); s = br.readLine();//放在下面 } br.close(); is.close(); in.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
BufferedWriter_test
package com.donghe.test; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import javax.print.attribute.standard.OutputDeviceAssigned; import org.omg.CORBA.portable.OutputStream; public class BufferedWriter_test { public static void main(String[] args) { // TODO Auto-generated method stub File file =new File("e://13.txt"); FileOutputStream os; try { os = new FileOutputStream(file); OutputStreamWriter writer=new OutputStreamWriter(os); BufferedWriter bw=new BufferedWriter(writer); bw.write("nihaowoh sihaoren");//可以为空格 bw.newLine(); bw.write("1231"); bw.flush(); writer.flush(); os.flush(); bw.close(); writer.close(); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
zip类型的文件挑选
//zip文件的遍历 package com.donghe.test; import java.io.File; public class Ergodic { public static void main(String[] args) { ergodicFile("e://"); } public static void ergodicFile(String filePath){//文件的路径哪个文件内的遍历 File file=new File(filePath); File[] abstractFilePath=file.listFiles();//abstractFilePath(files) //listFiles()这些路径名表示此抽象路径名表示的目录中的文件。 //listFiles得到的是抽象路径名 for (int i = 0; i < abstractFilePath.length; i++) { // System.out.println(files[i].getAbsolutePath()); if(abstractFilePath[i].isDirectory()){//判断遍历的文件是不是文件夹Directory目录 ergodicFile(abstractFilePath[i].getAbsolutePath());//绝对路径 递归 }else{ if(abstractFilePath[i].getName().endsWith(".zip")){//文件名字//绝对路径也行 System.out.println(abstractFilePath[i].getAbsolutePath()); } } } //getAbsolutePath根据路径名指定的当前驱动器目录(如果有)解析相对路径名,可使该路径名成为绝对路径名 //如果此抽象路径名是空抽象路径名,则返回当前用户目录的路径名字符串 //lastModified() 返回此抽象路径名表示的文件最后一次被修改的时间。 } }
package com.donghe.test; import java.io.File; import java.io.FilenameFilter; public class ZipFilenameFilter implements FilenameFilter{//FilenameFilter为接口 @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.endsWith(".zip");//如果含有.zip为true 否则为false } }
copyFile
//复制文件 package com.donghe.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class copyFile { public static void main(String[] args) { // TODO Auto-generated method stub copyFile("e://13.txt","e:14.txt"); } public static void copyFile(String path,String copy){ File source=new File(path);//源文件 File file=new File(copy);//copy文件 if(!source.exists()){ System.out.println("拷贝的源文件不存在"); return; } if(!file.exists()){ try { file.createNewFile();//如果不存在创建一个新的 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { FileInputStream sourceIn=new FileInputStream(source);//读 in FileOutputStream copyOut=new FileOutputStream(file);//写流 out byte[] array=new byte[4]; int i=0; while(i!=-1){ i=sourceIn.read(array);// if(i!=-1){ copyOut.write(array,0,i);//读几位写几位 } } sourceIn.close();//关闭 copyOut.flush();//冲刷 刷新缓冲区 copyOut.close(); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
FileInputStream_read
package com.donghe.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; public class FileInputStream_read { public static void main(String[] args) { File file = new File("e://13.txt"); FileInputStream in = null; try { in = new FileInputStream(file);//得到一个流输入流 读为输入流 byte[] array = new byte[4];//一般以2 4 8 16 32 64 128 256 512 数组就包含四个地址 指向四个数据 数据通过循环不断更改 int i = in.read(array);// 每次1024个字节去读 i=1024 //读取1024个字节写进数组里面 //读入缓冲区的字节总数 //比如512个汉字 则array[0]对应第一个汉字 array[1]对应第二个汉字 array[2]对应第三个汉字 以此类推 // System.out.println(new String(array));//根据数据内容构造成字符串 while (i != -1) { // i = in.read(array);// 放上面的话 最后如果字数少的话 i=-1 仍然会打印1024个字节 如果放在这里的话未读完i=-1会多输出一边 System.out.println(i); System.out.println(new String(array,0,i)); i = in.read(array);// 放上面的话 最后如果字数少的话 i=-1 仍然会打印1024个字节 } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
FileOutputStream_write
package com.donghe.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class FileOutputStream_write { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); String s = scanner.next(); File file = new File("e://22.txt"); FileOutputStream out = null; if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }// 文件创建 } try { out = new FileOutputStream(file);//字节 out.write(s.getBytes());// 写 将字符串变成字节 out.flush();// 刷新 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
相关文章推荐
- Xml解析器(java用的)
- Thinking in Java 笔记(第二章 一切都是对象)
- 【java】利用set集合特性去重
- bj java 初学 2015-07-23
- Eclipse远程调试(远程服务器端监听)
- JAVA设计模式之单例模式
- 解决eclipse中logcat不显示log的问题
- SpringTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)
- UBUNTU 配置JDK环境
- Java programer的成长之路
- MyBatis java and MySql local variables
- java 访问web项目的servlet
- JAVA 堆和栈
- java异常
- java 序列化
- 一个完全打印出xml文档的程序 [我真是无聊]
- 谈论高并发(三十)解析java.util.concurrent各种组件(十二) 认识CyclicBarrier栅栏
- JAVA测试初看。
- javah 命令找不到类文件的解决办法
- Java clone方法(下)