本文整理汇总了Java中java.io.OutputStreamWriter.close方法的典型用法代码示例。如果您正苦于以下问题:Java OutputStreamWriter.close方法的具体用法?Java OutputStreamWriter.close怎么用?Java OutputStreamWriter.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.OutputStreamWriter
的用法示例。
在下文中一共展示了OutputStreamWriter.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveDocument
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
public static void saveDocument(Document doc, String filePath) {
/**
* @todo: Configurable parameters
*/
try {
/*The XOM bug: reserved characters are not escaped*/
//Serializer serializer = new Serializer(new FileOutputStream(filePath), "UTF-8");
//serializer.write(doc);
OutputStreamWriter fw =
new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
fw.write(doc.toXML());
fw.flush();
fw.close();
}
catch (IOException ex) {
new ExceptionDialog(
ex,
"Failed to write a document to " + filePath,
"");
}
}
示例2: post
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
/**
* Sends a POST request to a given url, with JSON data as payload
*
* @param url
* @param data
* @return String
*/
public static String post(String uri, HashMap<String, String> payload) throws Exception {
URL url = new URL(uri);
Object connection = (uri.startsWith("https://") ? (HttpsURLConnection) url.openConnection()
: (HttpURLConnection) url.openConnection());
((URLConnection) connection).setConnectTimeout(8 * 1000);
((URLConnection) connection).setRequestProperty("User-Agent", userAgent);
((URLConnection) connection).setDoInput(true);
((URLConnection) connection).setDoOutput(true);
((HttpURLConnection) connection).setRequestMethod("POST");
((URLConnection) connection).setRequestProperty("Accept", "application/json");
((URLConnection) connection).setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(((URLConnection) connection).getOutputStream(), "UTF-8");
writer.write(mapToJson(payload));
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(((URLConnection) connection).getInputStream()));
StringBuffer data = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
data.append(line);
}
br.close();
((HttpURLConnection) connection).disconnect();
return data.toString();
}
示例3: post
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
public static String post(URLConnection connection,
String stringWriter,
Credentials credentials) throws Exception {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Authorization",
"Basic "
+ Base64.encode((credentials.getUserName() + ":" + new String(
credentials.getPassword())).getBytes()));
OutputStreamWriter postData = new OutputStreamWriter(connection.getOutputStream());
postData.write(stringWriter);
postData.flush();
postData.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = "";
String line = "";
while ((line = in.readLine()) != null)
response = response + line;
in.close();
return response;
}
示例4: saveContext
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
/**
* 保存文件
*
* @param articleno
* 小说编号
* @param chapterno
* 章节编号
* @param content
* 章节内容
* @throws IOException
* IO异常
*/
public static void saveContext(int articleno, int chapterno, String content) throws IOException {
String path = getTextFilePathByChapterno(articleno, chapterno);
File file = new File(path);
File parentPath = file.getParentFile();
if (!parentPath.exists()) {
parentPath.mkdirs();
}
try {
OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(file),
YiDuConstants.yiduConf.getString(YiDuConfig.TXT_ENCODING));
outputStream.write(content);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
示例5: createTestFile
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
/**
* Create an in-memory file with simple string content.
*
* @param content Content of the file.
*/
public FileObject createTestFile(String content) throws IOException {
FileObject root = FileUtil.createMemoryFileSystem().getRoot();
FileObject fo = root.createData(TEST_FILE_NAME);
OutputStream os = fo.getOutputStream();
try {
OutputStreamWriter osw = new OutputStreamWriter(os, TEST_FILE_ENC);
try {
osw.write(content);
} finally {
osw.flush();
osw.close();
}
} finally {
os.close();
}
return fo;
}
示例6: save
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
/**
* Saves result to report.txt according to the format
*
* <iteration>,<time>,<score>
*
* @throws IOException
*/
public void save() throws IOException {
if (fs.exists(file)) {
fs.delete(file, false);
}
FSDataOutputStream fsDataOutputStream = fs.create(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fsDataOutputStream);
BufferedWriter writer = new BufferedWriter(outputStreamWriter);
for (int i = 0; i < time.size(); i++) {
writer.write("" + i + "," + time.get(i) + "," + result.get(i) + "\n");
}
writer.flush();
writer.close();
outputStreamWriter.close();
fsDataOutputStream.close();
}
示例7: writeToFile
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
public static void writeToFile(String data, Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("imagenes.json", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
示例8: writeInternal
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
Charset charset = this.getCharset(outputMessage.getHeaders());
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
try {
if(this.jsonPrefix != null) {
writer.append(this.jsonPrefix);
}
this.gson.toJson(o, writer);
writer.close();
} catch (JsonIOException var7) {
throw new HttpMessageNotWritableException("Could not write JSON: " + var7.getMessage(), var7);
}
}
示例9: doInBackground
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
@Override
protected final Void doInBackground(ItemState... itemStates) {
for (ItemState state : itemStates) {
try {
HttpURLConnection urlConnection = ConnectionUtil.createUrlConnection(serverUrl + "/rest/items/" + state.mItemName + "/state");
try {
urlConnection.setRequestMethod("PUT");
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "text/plain");
urlConnection.setRequestProperty("Accept", "application/json");
OutputStreamWriter osw = new OutputStreamWriter(urlConnection.getOutputStream());
osw.write(state.mItemState);
osw.flush();
osw.close();
Log.v("Habpanelview", "set request response: " + urlConnection.getResponseMessage()
+ "(" + urlConnection.getResponseCode() + ")");
} finally {
urlConnection.disconnect();
}
} catch (IOException | GeneralSecurityException e) {
Log.e("Habpanelview", "Failed to set state for item " + state.mItemName, e);
}
if (isCancelled()) {
return null;
}
}
return null;
}
示例10: writedoc
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
/**
* Writes the given element's subtree to the specified file.
*
* @param fname The output file name
* @param ele The xml Element subtree to write to file
* @param doc The Document
* @return True if content previously existed in the given file and the content is the same
* as the new content provided
* @exception Hexception If exception
*/
private boolean writedoc(
String fname,
Element ele,
Document doc)
throws Hexception {
try {
boolean contentEquals = false;
String s1 = null;
File f = new File(fname);
if (f.exists())
s1 = Files.readFileToEncoding(f, "UTF-8").toString();
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
Writer wtr = new BufferedWriter(osw);
OutputFormat format = new OutputFormat(doc, "UTF-8", true);
// Indenting true
format.setMethod("xml");
// May not ne necessary to call this
format.setLineWidth(0);
// No line wrapping
XMLSerializer ser = new XMLSerializer(wtr, format);
ser.serialize(ele);
fos.close();
osw.close();
wtr.close();
if (s1 != null)
contentEquals = s1.contentEquals(Files.readFileToEncoding(f, "UTF-8"));
return contentEquals;
} catch (IOException ioe) {
throw new Hexception("cannot write file \"" + fname
+ "\" reason: " + ioe);
}
}
示例11: dump
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
/**
* Ascii file dump.
*
* dumpFile must not be null.
*/
private void dump(String varName,
File dumpFile) throws IOException, BadSpecial {
String val = shared.userVars.get(varName);
if (val == null)
throw new BadSpecial(SqltoolRB.plvar_undefined.getString(varName));
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(dumpFile), (shared.encoding == null)
? DEFAULT_FILE_ENCODING : shared.encoding);
try {
osw.write(val);
if (val.length() > 0) {
char lastChar = val.charAt(val.length() - 1);
if (lastChar != '\n' && lastChar != '\r') osw.write(LS);
}
osw.flush();
} finally {
try {
osw.close();
} catch (IOException ioe) {
// Intentionally empty
} finally {
osw = null; // Encourage GC of buffers
}
}
// Since opened in overwrite mode, since we didn't exception out,
// we can be confident that we wrote all the bytest in the file.
stdprintln(SqltoolRB.file_wrotechars.getString(
Long.toString(dumpFile.length()), dumpFile.toString()));
}
示例12: onClick
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bStart:
startRecording();
break;
case R.id.bStop:
stopRecording();
break;
case R.id.bExport:
try {
// create and write output file in cache directory
File outputFile = new File(getActivity().getCacheDir(), "recording.csv");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile));
writer.write(Util.recordingToCSV(mRecording));
writer.close();
// get Uri from FileProvider
Uri contentUri = FileProvider.getUriForFile(getActivity(), "com.martindisch.accelerometer.fileprovider", outputFile);
// create sharing intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
// temp permission for receiving app to read this file
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("text/csv");
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
} catch (IOException e) {
Toast.makeText(getActivity(), R.string.error_file, Toast.LENGTH_SHORT).show();
}
break;
}
}
示例13: createInfoFile
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
private void createInfoFile(Path dir) throws IOException
{
Path info = Files.createFile(dir.resolve("info.txt"));
Map<String, String> machineenv = DockerMachine.machineenv();
OutputStreamWriter out = new OutputStreamWriter(Files.newOutputStream(info, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
out.write("Java version = " + System.getProperty("java.version") + "\n");
out.write("Docker version = " + DockerContainer.version(machineenv) + "\n");
out.write("VBoxVersion version = " + DockerMachine.vboxversion() + "\n");
out.write("Machine Env = " + Arrays.toString(machineenv.entrySet().toArray()) + "\n");
out.close();
}
示例14: writeSrcFile
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
private void writeSrcFile(Path srcFilePath, String fileName, String data)
throws IOException {
OutputStreamWriter osw = getOutputStreamWriter(srcFilePath, fileName);
osw.write(data);
osw.close();
}
示例15: writeContent
import java.io.OutputStreamWriter; //导入方法依赖的package包/类
static void writeContent(HttpURLConnection urlConn, String content) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(
urlConn.getOutputStream());
out.write(content);
out.close();
}