java 中文件outputstream的操作注意

本文介绍了Java中文件的基本操作,包括文件的创建、读取、复制和修改等过程,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class FileTest {
	public static void main(String[] args) {
		File file = new File("F:\\3.txt");
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println(file.length());
		}

	}
}

 上面一段代码是很简单的文件创建操作。 

 下面,我们分析下面一段代码。

 

public class FileTest1 {
	public static void main(String[] args) {
		File file = new File("F:\\3.txt");
		System.out.println(file.length());
		OutputStream os;
		try {
			os = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println(file.length());
	}
}

 

结果为:  如果在硬盘上存在F:\\3.txt 这个文件, 则上述代码会将3.txt文件的大小(length)清0,相当于格式化。

 而如果在硬盘上不存在这个文件,则会新创建出一个大小为0的 3.txt文件。 

 如果文件存在时,使用上述代码要小心。

 

一般来说,我们在做输出流时, 文件路径是一个空的(或是硬盘上没有)文件,即当做目标文件。

普通的复制文件操作,用输入流将文件读取,将流加载至内存,在输出流中,循环将内存中的流写至目标文件。

 

对于文件内容的修改,我们常常使用字符流的方式,即Reader和Writer模式。

Reader 可以整行读取文件的内容,writer可以将字符串一次性写入目标文件。

 

下面为文件复制的代码:

public static void copy(String oldPath, String newPath) {
		try {
			int byteRead = 0;
			int byteSum = 0;
			File oldFile = new File(oldPath);
			if(oldFile.exists()) {
				FileInputStream is = new FileInputStream(oldFile);
				FileOutputStream os = new FileOutputStream(newPath);
				byte[] bt = new byte[345];
				while((byteRead = is.read(bt)) != -1) {
					byteSum += byteRead;
					System.out.println(byteSum);
					os.write(bt, 0, byteRead);
				}
				is.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

下面为文件修改的代码:

private String readFile(String filePath, DBConfigVO dbvo) {
		BufferedReader br = null;
		String line = null;
		StringBuffer buf = new StringBuffer();
		
		try {
			// 根据文件路径创建缓冲输入流
			br = new BufferedReader(new FileReader(filePath));
			// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
			while((line = br.readLine()) != null) {
				String[] a = new String[2];
				a = line.split(" ");
				
				for(Method m : DBConfigVO.class.getDeclaredMethods()) {
					String mName = m.getName();
					if(mName.startsWith("get")) {
						if(a[0].equals(mName.substring(3).toLowerCase())) {
								buf.append(line.substring(0, line.length() - a[1].length() - 1)).
										append(" ").append(m.invoke(dbvo).toString());
						}
					}
					if(mName.startsWith("is")) {
						if(a[0].equals(mName.substring(2).toLowerCase())) {
							buf.append(line.substring(0, line.length() - a[1].length() - 1)).
									append(" ").append(m.invoke(dbvo).toString());
						}
					}
					
				}
					
				buf.append(System.getProperty("line.separator"));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					br = null;
				}
			}
		}
		
		return buf.toString();
	}
	
	/**  
     * 将内容回写到文件中  
     *   
     * @param filePath  
     * @param content  
     */
	private void writeFile(String content, String filePath) {
		BufferedWriter bw = null;
		
		try {
			// 根据文件路径创建缓冲输出流
			bw = new BufferedWriter(new FileWriter(filePath));
			// 将内容写入文件中
			bw.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					bw = null;
				}
			}
		}
	}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值