Search in sources :

Example 81 with OutputStreamWriter

use of java.io.OutputStreamWriter in project hazelcast by hazelcast.

the class HTTPCommunicator method mapPut.

public int mapPut(String mapName, String key, String value) throws IOException {
    final String url = address + "maps/" + mapName + "/" + key;
    final HttpURLConnection urlConnection = setupConnection(url, "POST");
    // post the data
    OutputStream out = urlConnection.getOutputStream();
    Writer writer = new OutputStreamWriter(out, "UTF-8");
    writer.write(value);
    writer.close();
    out.close();
    return urlConnection.getResponseCode();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 82 with OutputStreamWriter

use of java.io.OutputStreamWriter in project hazelcast by hazelcast.

the class HTTPCommunicator method doPost.

private static ConnectionResponse doPost(String url, String... params) throws IOException {
    HttpURLConnection urlConnection = setupConnection(url, "POST");
    // post the data
    OutputStream out = urlConnection.getOutputStream();
    Writer writer = new OutputStreamWriter(out, "UTF-8");
    String data = "";
    for (String param : params) {
        data += URLEncoder.encode(param, "UTF-8") + "&";
    }
    writer.write(data);
    writer.close();
    out.close();
    try {
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[4096];
        int readBytes = inputStream.read(buffer);
        return new ConnectionResponse(readBytes == -1 ? "" : new String(buffer, 0, readBytes), urlConnection.getResponseCode());
    } finally {
        urlConnection.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 83 with OutputStreamWriter

use of java.io.OutputStreamWriter in project HanLP by hankcs.

the class TestDependencyCorpus method testMakeCRF.

/**
     * 导出CRF训练语料
     *
     * @throws Exception
     */
public void testMakeCRF() throws Exception {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Tools\\CRF++-0.58\\example\\dependency\\dev.txt")));
    LinkedList<CoNLLSentence> coNLLSentences = CoNLLLoader.loadSentenceList("D:\\Doc\\语料库\\依存分析训练数据\\THU\\dev.conll.fixed.txt");
    for (CoNLLSentence coNLLSentence : coNLLSentences) {
        for (CoNLLWord coNLLWord : coNLLSentence.word) {
            bw.write(coNLLWord.NAME);
            bw.write('\t');
            bw.write(coNLLWord.CPOSTAG);
            bw.write('\t');
            bw.write(coNLLWord.POSTAG);
            bw.write('\t');
            int d = coNLLWord.HEAD.ID - coNLLWord.ID;
            int posDistance = 1;
            if (// 在后面
            d > 0) {
                for (int i = 1; i < d; ++i) {
                    if (coNLLSentence.word[coNLLWord.ID - 1 + i].CPOSTAG.equals(coNLLWord.HEAD.CPOSTAG)) {
                        ++posDistance;
                    }
                }
            } else {
                for (// 在前面
                int i = 1; // 在前面
                i < -d; // 在前面
                ++i) {
                    if (coNLLSentence.word[coNLLWord.ID - 1 - i].CPOSTAG.equals(coNLLWord.HEAD.CPOSTAG)) {
                        ++posDistance;
                    }
                }
            }
            bw.write((d > 0 ? "+" : "-") + posDistance + "_" + coNLLWord.HEAD.CPOSTAG);
            bw.newLine();
        }
        bw.newLine();
    }
    bw.close();
}
Also used : CoNLLWord(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord) FileOutputStream(java.io.FileOutputStream) CoNLLSentence(com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 84 with OutputStreamWriter

use of java.io.OutputStreamWriter in project HanLP by hankcs.

the class TestICWB method testDumpPeople2014ToBEMS.

public void testDumpPeople2014ToBEMS() throws Exception {
    final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Tools\\CRF++-0.58\\example\\seg_cn\\2014.txt")));
    CorpusLoader.walk("D:\\JavaProjects\\CorpusToolBox\\data\\2014", new CorpusLoader.Handler() {

        @Override
        public void handle(Document document) {
            List<List<Word>> simpleSentenceList = document.getSimpleSentenceList();
            for (List<Word> wordList : simpleSentenceList) {
                try {
                    for (Word word : wordList) {
                        bw.write(word.value);
                        bw.write(' ');
                    }
                    bw.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    bw.close();
}
Also used : Word(com.hankcs.hanlp.corpus.document.sentence.word.Word) IWord(com.hankcs.hanlp.corpus.document.sentence.word.IWord) FileOutputStream(java.io.FileOutputStream) CorpusLoader(com.hankcs.hanlp.corpus.document.CorpusLoader) OutputStreamWriter(java.io.OutputStreamWriter) List(java.util.List) LinkedList(java.util.LinkedList) IOException(java.io.IOException) Document(com.hankcs.hanlp.corpus.document.Document) BufferedWriter(java.io.BufferedWriter)

Example 85 with OutputStreamWriter

use of java.io.OutputStreamWriter in project Gaffer by gchq.

the class AddElementsFromHdfsIT method shouldThrowExceptionWhenAddElementsFromHdfsWhenFailureDirectoryContainsFiles.

@Test
public void shouldThrowExceptionWhenAddElementsFromHdfsWhenFailureDirectoryContainsFiles() throws Exception {
    final FileSystem fs = FileSystem.getLocal(createLocalConf());
    fs.mkdirs(new Path(failureDir));
    try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fs.create(new Path(failureDir + "/someFile.txt"), true)))) {
        writer.write("Some content");
    }
    try {
        addElementsFromHdfs(ByteEntityKeyPackage.class);
        fail("Exception expected");
    } catch (final OperationException e) {
        assertEquals("Failure directory is not empty: " + failureDir, e.getCause().getMessage());
    }
    //Previous job will output data successfully to the output dir but not load it.
    fs.delete(new Path(outputDir), true);
    try {
        addElementsFromHdfs(ClassicKeyPackage.class);
        fail("Exception expected");
    } catch (final OperationException e) {
        assertEquals("Failure directory is not empty: " + failureDir, e.getCause().getMessage());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) OutputStreamWriter(java.io.OutputStreamWriter) OperationException(uk.gov.gchq.gaffer.operation.OperationException) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Aggregations

OutputStreamWriter (java.io.OutputStreamWriter)1784 IOException (java.io.IOException)690 FileOutputStream (java.io.FileOutputStream)653 BufferedWriter (java.io.BufferedWriter)647 Writer (java.io.Writer)479 File (java.io.File)407 PrintWriter (java.io.PrintWriter)296 InputStreamReader (java.io.InputStreamReader)244 OutputStream (java.io.OutputStream)221 ByteArrayOutputStream (java.io.ByteArrayOutputStream)216 BufferedReader (java.io.BufferedReader)210 Test (org.junit.Test)129 InputStream (java.io.InputStream)112 FileNotFoundException (java.io.FileNotFoundException)104 ArrayList (java.util.ArrayList)96 Path (org.apache.hadoop.fs.Path)93 UnsupportedEncodingException (java.io.UnsupportedEncodingException)92 URL (java.net.URL)90 HttpURLConnection (java.net.HttpURLConnection)78 FileInputStream (java.io.FileInputStream)71