Create a file and write to

File file1 = null;
	if (!file.isFile()) {
		file1 = new File("E:\\a\\aa\\b\\test.txt");
		boolean createNewFile = false;
		try {
			createNewFile = file1.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(createNewFile);
	}
	String str = "这些都将写入文件中";
	byte[] bb = str.getBytes(); // 将字符串转换成字节数
	OutputStream out = null;
	try {
		out = new FileOutputStream(file1); // 实例化OutpurStream
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	// 写入
	try {
		out.write(bb); // 写入
		out.close(); // 关闭
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	System.out.println("----------------------------------------");
Published 14 original articles · won praise 1 · views 237

Guess you like

Origin blog.csdn.net/weixin_45061669/article/details/104830122