J2SE学习笔记(七)

/*
	java的IO操作(传统IO部分)
	基于java.io.*
	
	java流的分类:
	1、输入流、输入流——这里的输入和输出主要是相对内存而言;
	2、字节流、字符流——字节流直接操作文件,而字符流则利用了缓冲区;
	3、节点流、包装流——节点流直接从一个特定的IO设备读写数据,而包装流(处理流)则对一个已经存在的流进行连接或封装,通过封装后的流来实现数据的读写。

J2SE学习笔记(七)_第1张图片

	
		
	节点流:
	java.io.File类
*/
	public void createFile() throws IOException{
		File f=new File("test.txt");
		f.createNewFile();
	}

	//创建文件夹
	public void createDir() throws IOException{
		File f=new File("H:"+File.separator+"test");
		f.mkdir();
	}

	//删除文件
	public void deleteFile(){
		File f=new File("test.txt");
		f.delete();
	}

	//文件(夹)遍历
	public void listFile(){
		File f=new File("H:"+File.separator);
		String[] files=f.list();
		for (String file : files) {
			System.out.println(file);
		}
	}

	//文件过滤
	public void listFiles(){
		File f=new File("H:"+File.separator+"casperJs");
		File[] files=f.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				if(name.toLowerCase().endsWith(".js")){
					return true;
				}else{
					return false;
				}
			}
		});
		for (File file : files) {
			System.out.println(file.getName());
		}
	}

		
	//使用RandomAccessFile读取、写入文件
	//使用RandomAccessFile向数据库写入中文的时候,
    //——使用write(String.getBytes()), 能够正常写入
    //——使用writeBytes(String), writeChars(String), writeUTF(String)均产生乱码。
	
	//写入
	public void randomWriteFile() throws IOException{
		RandomAccessFile raf=new RandomAccessFile("test.txt", "rw");
		raf.write("You are my pretty sunshine\n没你的世界好好坏坏只是无味空白\n答应我哪天走失了人海\n一定站在最显眼路牌\n等着我 一定会来\n".getBytes());
		raf.close();
	}

	//追加
	public void randomWriteFileAnywhere() throws IOException{
		RandomAccessFile raf=new RandomAccessFile("test.txt", "rw");
		raf.seek(raf.length());
		raf.write("You are the pretty sunshine of my life\n等着我不要再离开\n".getBytes());
		raf.close();
	}
	
	//读取
	public void randomReadFile() throws IOException{
		RandomAccessFile raf=new RandomAccessFile("test.txt", "r");
		while(raf.getFilePointer()<raf.length()){
			System.out.println(new String(raf.readLine().getBytes("ISO-8859-1"),"utf-8"));
		}
		raf.close();
	}

		//字节流
	//字节流访问文件 FileInputStream、FileOutputStream
	//写入
	public void FileOutputStreamTest() throws IOException{
		FileOutputStream fos=new FileOutputStream("test.txt");
		byte[] b=(("You are my pretty sunshine\n"
				+"没你的世界好好坏坏只是无味空白\n"
				+"答应我哪天走失了人海\n"
				+"一定站在最显眼路牌\n"
				+"等着我 一定会来\n").getBytes());
		fos.write(b);
		fos.close();
	}
	
	//追加
	public void FileOutputStreamAddTest() throws IOException{
		FileOutputStream fos=new FileOutputStream("test.txt",true);
		byte[] b=("You are the pretty sunshine of my life\n等着我不要再离开\n".getBytes());
		fos.write(b);
		fos.close();
	}
	
	//读取
	public void FileInputStreamTest() throws IOException{
		FileInputStream fis=new FileInputStream("test.txt");
		InputStreamReader isr=new InputStreamReader(fis, "UTF-8");//利用转换流(InputStreamReader)将字节流转换成字符流
		char[] buff=new char[4];
		while(isr.read(buff)>0){//到达结尾时返回-1
				System.out.print(new String(buff));
		}
		fis.close();
	}
	
	//字符流
	//字符流访问文件 FileReader、FileWriter
	//写入
	public void FileWriterTest() throws IOException{
		FileWriter fw=new FileWriter("test.txt");
		fw.write("You are my pretty sunshine\n"
					+"没你的世界好好坏坏只是无味空白\n"
					+"答应我哪天走失了人海\n"
					+"一定站在最显眼路牌\n"
					+"等着我 一定会来\n");
		fw.close();
	}
	
	//追加
	public void FileWriterAddTest() throws IOException{
		FileWriter fw=new FileWriter("test.txt",true);
		fw.write("You are the pretty sunshine of my life\n等着我不要再离开\n");
		fw.close();
	}
	
	//读取
	public void FileReaderTest() throws IOException{
		FileReader fr=new FileReader("test.txt");
		char[] cbuf=new char[8];
		while(fr.read(cbuf)>0){
			System.out.print(new String(cbuf));
		}
		fr.close();
	}

	//BufferedReader、BufferedWriter缓冲流
	//包装成缓冲流读取文件(BufferedReader有readline()方法,可以一次读取一行)
	public void bufferedTest() throws IOException{
		FileReader fr=new FileReader("test.txt");
		BufferedReader br=new BufferedReader(fr);
		String content=null;
		while((content=br.readLine()) != null){
			System.out.println(content);
		}
		br.close();
	}
	
	//重定向标准输入/输出
	public void redirectOut() throws IOException{
		PrintStream ps=new PrintStream("input.txt");
		System.setOut(ps);//将控制台输出重定向到input.txt文件中
		System.out.println("重定向后的标准输出流");
	}
	
	//java虚拟机读取其它进程数据
	public void readFromProcess() throws IOException{
		Process p=Runtime.getRuntime().exec("node -v");//这里是获取本机nodejs的版本号
		BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
		String content=null;
		while((content=br.readLine()) != null){
			System.out.println(content);
		}
		br.close();
	}


你可能感兴趣的:(java,IO,J2SE)