书小宅之java——磁盘上多种数据类型的文件读写

创建文件并向文件中写数据

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class File {
	public static void main(String[] args){
		List<String> list=new ArrayList<String>();
		list.add("小熊");
		list.add("大猫");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		} 
		
		try{
			FileWriter fw=new FileWriter("C:\\Users\\yangxue\\Desktop\\test\\name.txt");
			for(String s:list){
				fw.write(s);
				fw.write("\r\n");
			}
			fw.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.sound.sampled.Line;
import javax.swing.text.AbstractDocument.Content;

import org.omg.CORBA.PUBLIC_MEMBER;

public class fileProject {
	private static String path="C:\\Users\\yangxue\\Desktop\\test\\";
	private static String tempFile;
	public static List listContent(){
		List<String> list=new ArrayList<String>();
		list.add("小熊");
		list.add("大猫");
		return list;
	}
	public static Map mapContent(){
		Map<String,String> map= new HashMap<>();
		map.put("20161123", "张三");
		map.put("20161124", "李四");
		
		Set<String> set =map.keySet();
		Iterator<String> it =set.iterator();
		while(it.hasNext())
			System.out.println(it.next());
		System.out.println("");
		
		Collection <String> coll=map.values();
		it=coll.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
		return map;
	}
	public static String stringContent(String s){
		String content ="hello world ! "+"\r\n"+s;
		return content;
	}
	public static String splitContent(String content){
		String[] ss = new String[20];
		ss=content.split("#");
		String outString="";
		for(String s:ss){
			outString += s+" ";
		}
		return outString;
	}
	public static String SubstringContent(String content){
		String outString="";
		//将字符串从索引号为2开始街区,一直到字符串的末尾【索引从0开始】
		outString=content.substring(2);
		//索引号(x>=2 && x<5)
		outString=content.substring(2,5);
		return outString;
	}
	public static boolean createFile(String filePath,String filecontent){
		File file= new File(filePath);
		try{
			if(!file.exists()){
				file.createNewFile();
				//删除文件
				//file.delete();
				writeFileContent(filePath,filecontent);
			}
			else writeFileContent(filePath,filecontent);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}
	private static void writeFileContent(String filePath, String filecontent) throws IOException {
		FileInputStream fis = null;
		InputStreamReader isr =null;
		BufferedReader br = null;
		FileOutputStream fos = null;
		PrintWriter pw = null;
		
		String temp ="";
		try{
			File file=new File(filePath);
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer buffer =new StringBuffer();
			
			//保存文件原有的内容
			for(int i=0;(temp=br.readLine())!=null;i++){
				buffer.append(temp);
				buffer.append(System.getProperty("line.separator"));
			}
			buffer.append(listContent());
			buffer.append(System.getProperty("line.separator"));
			buffer.append(mapContent());
			buffer.append(System.getProperty("line.separator"));
			buffer.append(stringContent("end"));
			buffer.append(System.getProperty("line.separator"));
			buffer.append(splitContent("split#string#function"));
			buffer.append(System.getProperty("line.separator"));
			buffer.append(SubstringContent("你 们 好"));
			
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buffer.toString().toCharArray());
			pw.flush();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(pw!=null) pw.close();
			if(fos!=null) fos.close();
			if(br!=null) br.close();
			if(isr!=null) isr.close();
			if(fis!=null) fis.close();
		}
	}
	public static void main(String[] args) throws IOException{
		String filePath=path+"name"+".txt";
		createFile(filePath,"nihao");
	}
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值