用Java写脚本,常用的一些方法

您所在的位置:网站首页 java清除变量 用Java写脚本,常用的一些方法

用Java写脚本,常用的一些方法

2023-07-05 10:30| 来源: 网络整理| 查看: 265

/** * 写文件 * * @param filePath * @param sets * @throws IOException */public synchronized void writeFile(String filePath, String content) throws IOException { FileWriter fw = new FileWriter(filePath); PrintWriter out = new PrintWriter(fw); out.write(content); out.println(); fw.close(); out.close();}

/** * 读文件 * * @param filename * @return */public static String readFile(String filepath) { File file = new File(filepath); InputStream inputStream = null; BufferedReader bufferedReader = null; try { inputStream = new FileInputStream(file); String content = ""; if (inputStream != null) { bufferedReader = new BufferedReader(new InputStreamReader( inputStream)); String line = ""; while ((line = bufferedReader.readLine()) != null) { content += line; } } return content; } catch (Exception e) { System.out.println(e.toString()); } finally { try { if (bufferedReader != null) { bufferedReader.close(); bufferedReader = null; } if (inputStream != null) { inputStream.close(); inputStream = null; } } catch (Exception e) { System.out.println(e.toString()); } }

return null;}

public static byte[] readByte(final InputStream in) throws IOException { ByteArrayOutputStream output = null; try { if (in == null) { return null; } output = new ByteArrayOutputStream(1024 * 2); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { output.write(buffer, 0, len); } return output.toByteArray(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }}

public static boolean saveObject(Serializable serializable, String filePath) { try { FileOutputStream fout = new FileOutputStream(filePath); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(serializable); oos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false;}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3