Example usage for java.io OutputStreamWriter close

List of usage examples for java.io OutputStreamWriter close

Introduction

In this page you can find the example usage for java.io OutputStreamWriter close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:at.florian_lentsch.expirysync.net.JsonCaller.java

private void writeParams(HttpURLConnection connection, JSONObject params)
        throws IOException, JSONException, UnsupportedEncodingException {
    OutputStream os = connection.getOutputStream();

    final OutputStreamWriter osw = new OutputStreamWriter(os);
    final String toWriteOut = params.toString();

    osw.write(toWriteOut);//from w  w  w .  j a  v  a 2s .c  o m
    osw.close();
}

From source file:com.culvereq.vimp.networking.ConnectionHandler.java

public ServiceRecord addServiceRecord(TempServiceRecord serviceRecord) throws IOException, JSONException {
    URL requestURL = new URL(serviceURL + "add");
    HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection();
    connection.setDoInput(true);//  w w w .ja v  a2  s  . c  o m
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    String urlParameters = "";
    urlParameters += "key=" + Globals.access_key;
    urlParameters += "&vehicle-id=" + serviceRecord.getParentVehicle().getId();
    urlParameters += "&type-id=" + serviceRecord.getType().getValue();
    urlParameters += "&service-desc=" + serviceRecord.getDescription();
    urlParameters += "&service-date=" + serviceRecord.getServiceDate().getMillis() / 1000L;
    urlParameters += "&service-mileage=" + serviceRecord.getMileage();
    connection.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length);
    connection.setUseCaches(false);
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(urlParameters);
    writer.flush();
    writer.close();

    int responseCode = connection.getResponseCode();
    if (responseCode == 201) {
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject json = new JSONObject(response.toString());
        return new Deserializer().deserializeService(json, response.toString());
    } else if (responseCode == 400) {
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        throw new IOException(response.toString());
    } else {
        throw new IOException("Got response code: " + responseCode);
    }
}

From source file:com.example.kevinrotairo.androidcodecamp.GetLocationActivity.java

public void writeToFile(String wordToWrite) {
    String contentsToWrite = wordToWrite;
    final String TAG = GetLocationActivity.class.getName();
    try {//  w w  w  .j  a  va 2  s.c o  m
        //OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE));
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                openFileOutput(Constants.FILENAME, Context.MODE_APPEND));
        outputStreamWriter.write(contentsToWrite);
        outputStreamWriter.write("\n");
        outputStreamWriter.close();
        //Toast.makeText(getApplicationContext(), "File successfully saved!", Toast.LENGTH_LONG).show();
        //readFromFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.culvereq.vimp.networking.ConnectionHandler.java

public Vehicle addVehicle(TempVehicle vehicle) throws IOException, JSONException {
    URL requestURL = new URL(vehicleURL + "add");
    HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection();
    connection.setDoInput(true);//from   www  .j a va2s  .c  om
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    String urlParameters = "";
    urlParameters += "key=" + Globals.access_key;
    urlParameters += "&vehicle-name=" + vehicle.getName();
    urlParameters += "&vehicle-type-id=" + vehicle.getType().getValue();
    urlParameters += "&vehicle-make=" + vehicle.getMake();
    urlParameters += "&vehicle-model" + vehicle.getModel();
    urlParameters += "&vehicle-year" + vehicle.getYear();
    urlParameters += "&vehicle-mileage" + vehicle.getMileage();
    urlParameters += "&vehicle-vin" + vehicle.getVin();
    urlParameters += "&vehicle-license-no" + vehicle.getLicenseNumber();

    connection.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length);
    connection.setUseCaches(false);
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(urlParameters);
    writer.flush();
    writer.close();

    int responseCode = connection.getResponseCode();
    if (responseCode == 201) {
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject json = new JSONObject(response.toString());
        return new Deserializer().deserializeVehicle(json, response.toString());
    } else if (responseCode == 400) {
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        throw new IOException(response.toString());
    } else {
        throw new IOException("Got response code: " + responseCode);
    }
}

From source file:com.paramedic.mobshaman.activities.ActualizarInformacionActivity.java

private void updateFile(String fileName, ArrayList<String> inputs) {

    FileOutputStream outputStream;

    try {//from   w w w  .  j ava  2s .c  o m
        String separator = System.getProperty("line.separator");
        outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
        OutputStreamWriter osw = new OutputStreamWriter(outputStream);

        for (String input : inputs) {
            osw.append(input);
            osw.append(separator);
        }

        osw.flush();
        osw.close();
        outputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.github.nethad.clustermeister.provisioning.cli.Provisioning.java

private void createDefaultConfiguration(String configFilePath) {
    final File configFile = new File(configFilePath);
    if (configFile.exists()) {
        return;/*from  ww  w  . j  ava  2s.  c  o m*/
    }
    configFile.getParentFile().mkdirs();

    OutputSupplier<OutputStreamWriter> writer = Files.newWriterSupplier(configFile, Charsets.UTF_8);
    OutputStreamWriter output = null;
    try {
        output = writer.getOutput();
        output.append(YamlConfiguration.defaultConfiguration());
        output.flush();
        output.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ex) {
                // ignore
            }
        }
    }
}

From source file:com.bcp.bcp.geofencing.GeofenceTransitionsIntentService.java

public File saveGeoFile(String address, String status, String date, String mail, String geofile) {

    String textToSave = address + "," + status + "," + date + "," + mail;
    File myFile = null;//  w  ww. ja  va 2s  .  c  o  m
    try {
        myFile = new File("/sdcard/" + geofile);
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(textToSave);
        myOutWriter.close();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("saveGeoFile ", textToSave);

    return myFile;
}

From source file:org.ala.preprocess.ColFamilyNamesProcessor.java

/**
 * Copy the raw file into the repository.
 * /*www  .  j av a 2  s. c o  m*/
 * @param filePath
 * @param uri
 * @param infosourceUri
 * @param mimeType
 * @return
 * @throws FileNotFoundException
 * @throws Exception
 * @throws IOException
 */
private int copyRawFileToRepo(String filePath, String uri, String infosourceUri, String mimeType)
        throws FileNotFoundException, Exception, IOException {
    InfoSource infoSource = infoSourceDAO.getByUri(infosourceUri);

    Reader ir = new FileReader(filePath);
    DocumentOutputStream dos = repository.getDocumentOutputStream(infoSource.getId(), uri, mimeType);

    //write the file to RAW file in the repository
    OutputStreamWriter w = new OutputStreamWriter(dos.getOutputStream());

    //read into buffer
    char[] buff = new char[1000];
    int read = 0;
    while ((read = ir.read(buff)) > 0) {
        w.write(buff, 0, read);
    }
    w.flush();
    w.close();
    return dos.getId();
}

From source file:com.github.davidcarboni.encryptedfileupload.StreamingTest.java

private byte[] newShortRequest() throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final OutputStreamWriter osw = new OutputStreamWriter(baos, "US-ASCII");
    osw.write(getHeader("field"));
    osw.write("123");
    osw.write("\r\n");
    osw.write(getFooter());/*ww w. jav  a  2  s. c  o m*/
    osw.close();
    return baos.toByteArray();
}

From source file:com.cubusmail.server.services.CubusmailServlet.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String fileName = request.getSession().getServletContext().getRealPath(request.getServletPath());
    BufferedReader reader = new BufferedReader(new FileReader(fileName));

    OutputStream outputStream = response.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(outputStream);
    char[] inBuf = new char[1024];
    int len = 0;/*from w w  w.  j ava2 s  .c o m*/
    try {
        while ((len = reader.read(inBuf)) > 0) {
            writer.write(inBuf, 0, len);
        }
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
    }

    writer.flush();
    writer.close();
    outputStream.flush();
    outputStream.close();
}