java读取txt文件中的内容

java读取txt文档中的内容:在这个地方我们输入一个txt文件的路径,就可以读取出txt文档中的内容:

	public static String readStringFromtxt(String txtpath) {
		File file = new File(txtpath);
		StringBuilder result = new StringBuilder();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String s = null;
			while ((s = br.readLine()) != null) {
				result.append(System.lineSeparator() + s);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}

上面是对txt文档中的内容的读取,现在需要做的事情是对txt文档中的内容的写入,如何向txt文档中写入内容,这地方我们也是仅仅提供一个txt的路径,和要写入的字符串

	public static int writeStringToTxt(String targetTxt, String str) {
		File file = new File(targetTxt);
		BufferedWriter bwriter;
		try {
			bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
			bwriter.write(str);
			bwriter.flush();
			bwriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return 0;
	}

如何将pdf文件转换为base64的编码,在传递文件的时候我们常常需要做的事情是将pdf文件转换base64的编码,如何实现这个转换,这个地方我们也是仅仅提供一个pdf文档的地址,就可以实现将其转换为base64编码:

public static String pdfFileToBase64String(String pdfpath) {
		File file = new File(pdfpath);
		FileInputStream fin = null;
		BufferedInputStream bin = null;
		ByteArrayOutputStream baos = null;
		BufferedOutputStream bout = null;
		try {
			fin = new FileInputStream(file);// 建立读取文件的文件输出流
			bin = new BufferedInputStream(fin);// 在文件输出流上安装节点流(更大效率读取)
			baos = new ByteArrayOutputStream();// 创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量
			bout = new BufferedOutputStream(baos);// 创建一个新的缓冲输出流,以将数据写入指定的底层输出流
			byte[] buffer = new byte[1024];
			int len = bin.read(buffer);
			while (len != -1) {
				bout.write(buffer, 0, len);
				len = bin.read(buffer);
			}
			// 刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
			bout.flush();
			byte[] bytes = baos.toByteArray();
			// sun公司的API
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encodeBuffer(bytes).trim();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fin.close();
				bin.close();
				bout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

这上面实现的功能是给出一个pdf路径,将pdf文件转换为base64的编码,现在我们继续要做的事情是将base64编码转换为pdf文件:

	public static int base64ToPdf(String base64String, String pdfpath) {
		BufferedInputStream bin = null;
		FileOutputStream fout = null;
		BufferedOutputStream bout = null;
		BASE64Decoder decoder=new BASE64Decoder();
		try {
			byte[] bytes = decoder.decodeBuffer(base64String);
			// 创建一个将bytes作为其缓冲区的ByteArrayInputStream对象
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			// 创建从底层输入流中读取数据的缓冲输入流对象
			bin = new BufferedInputStream(bais);
			// 指定输出的文件
			File file = new File(pdfpath);
			// 创建到指定文件的输出流
			fout = new FileOutputStream(file);
			// 为文件输出流对接缓冲输出流对象
			bout = new BufferedOutputStream(fout);

			byte[] buffers = new byte[1024];
			int len = bin.read(buffers);
			while (len != -1) {
				bout.write(buffers, 0, len);
				len = bin.read(buffers);
			}

			bout.flush();// 刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bin.close();
				fout.close();
				bout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

上面就是我们在这个地方需要将的,java对txt文件的操作和如何将pdf转换为base64如何将base64转换为pdf文件。当然这个需要用到的base64的jar sun.misc.base64decoder.jar本想上传的但是,因为CSDN上面已经存在了这个资源文件当然也好找。

希望对你有所帮助!

猜你喜欢

转载自blog.csdn.net/datouniao1/article/details/80102900