URLConnection extends java.net.URLConnection { @Override public OutputStream getOutputStream() throws IOException { connect(); if (getDoOutput()) { OutputStream os = new FileOutputStream(this.file); if (getUseCaches()) { os = new BufferedOutputStream(os); } return os; } throw new UnknownServiceException(Locale.getString("E1")); } protected  URLConnection(URL url); @Override String getHeaderField(int index); @Override String getHeaderField(String name); @Override String getHeaderFieldKey(int index); @Override Map<String, List<String>> getHeaderFields(); @Override void connect(); @Override OutputStream getOutputStream(); @Override InputStream getInputStream();  }
@Test public void getOutputStream() throws IOException { File tmpFile = File.createTempFile("unittest", ".txt"); tmpFile.deleteOnExit(); URLConnection con = new URLConnection(tmpFile.toURI().toURL()); con.setDoOutput(true); try (OutputStream os = con.getOutputStream()) { try (OutputStreamWriter osw = new OutputStreamWriter(os)) { try (BufferedWriter bw = new BufferedWriter(osw)) { bw.write("HELLO WORLD!"); } } } assertEquals(12, tmpFile.length()); String line; try (BufferedReader br = new BufferedReader(new FileReader(tmpFile))) { line = br.readLine(); } assertEquals("HELLO WORLD!", line); }