Search in sources :

Example 71 with OutputStreamWriter

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

the class TexturePacker method writePackFile.

private void writePackFile(File outputDir, String scaledPackFileName, Array<Page> pages) throws IOException {
    File packFile = new File(outputDir, scaledPackFileName + settings.atlasExtension);
    File packDir = packFile.getParentFile();
    packDir.mkdirs();
    if (packFile.exists()) {
        // Make sure there aren't duplicate names.
        TextureAtlasData textureAtlasData = new TextureAtlasData(new FileHandle(packFile), new FileHandle(packFile), false);
        for (Page page : pages) {
            for (Rect rect : page.outputRects) {
                String rectName = Rect.getAtlasName(rect.name, settings.flattenPaths);
                for (Region region : textureAtlasData.getRegions()) {
                    if (region.name.equals(rectName)) {
                        throw new GdxRuntimeException("A region with the name \"" + rectName + "\" has already been packed: " + rect.name);
                    }
                }
            }
        }
    }
    Writer writer = new OutputStreamWriter(new FileOutputStream(packFile, true), "UTF-8");
    for (Page page : pages) {
        writer.write("\n" + page.imageName + "\n");
        writer.write("size: " + page.imageWidth + "," + page.imageHeight + "\n");
        writer.write("format: " + settings.format + "\n");
        writer.write("filter: " + settings.filterMin + "," + settings.filterMag + "\n");
        writer.write("repeat: " + getRepeatValue() + "\n");
        page.outputRects.sort();
        for (Rect rect : page.outputRects) {
            writeRect(writer, page, rect, rect.name);
            Array<Alias> aliases = new Array(rect.aliases.toArray());
            aliases.sort();
            for (Alias alias : aliases) {
                Rect aliasRect = new Rect();
                aliasRect.set(rect);
                alias.apply(aliasRect);
                writeRect(writer, page, aliasRect, alias.name);
            }
        }
    }
    writer.close();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) FileOutputStream(java.io.FileOutputStream) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) ImageWriter(javax.imageio.ImageWriter) Writer(java.io.Writer)

Example 72 with OutputStreamWriter

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

the class FilesTest method create.

@Override
public void create() {
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
    batch = new SpriteBatch();
    if (Gdx.files.isExternalStorageAvailable()) {
        message += "External storage available\n";
        message += "External storage path: " + Gdx.files.getExternalStoragePath() + "\n";
        try {
            InputStream in = Gdx.files.internal("data/cube.obj").read();
            StreamUtils.closeQuietly(in);
            message += "Open internal success\n";
        } catch (Throwable e) {
            message += "Couldn't open internal data/cube.obj\n" + e.getMessage() + "\n";
        }
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(Gdx.files.external("test.txt").write(false)));
            out.write("test");
            message += "Write external success\n";
        } catch (GdxRuntimeException ex) {
            message += "Couldn't open externalstorage/test.txt\n";
        } catch (IOException e) {
            message += "Couldn't write externalstorage/test.txt\n";
        } finally {
            StreamUtils.closeQuietly(out);
        }
        try {
            InputStream in = Gdx.files.external("test.txt").read();
            StreamUtils.closeQuietly(in);
            message += "Open external success\n";
        } catch (Throwable e) {
            message += "Couldn't open internal externalstorage/test.txt\n" + e.getMessage() + "\n";
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(Gdx.files.external("test.txt").read()));
            if (!in.readLine().equals("test"))
                message += "Read result wrong\n";
            else
                message += "Read external success\n";
        } catch (GdxRuntimeException ex) {
            message += "Couldn't open externalstorage/test.txt\n";
        } catch (IOException e) {
            message += "Couldn't read externalstorage/test.txt\n";
        } finally {
            StreamUtils.closeQuietly(in);
        }
        if (!Gdx.files.external("test.txt").delete())
            message += "Couldn't delete externalstorage/test.txt";
    } else {
        message += "External storage not available";
    }
    if (Gdx.files.isLocalStorageAvailable()) {
        message += "Local storage available\n";
        message += "Local storage path: " + Gdx.files.getLocalStoragePath() + "\n";
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(Gdx.files.local("test.txt").write(false)));
            out.write("test");
            message += "Write local success\n";
        } catch (GdxRuntimeException ex) {
            message += "Couldn't open localstorage/test.txt\n";
        } catch (IOException e) {
            message += "Couldn't write localstorage/test.txt\n";
        } finally {
            StreamUtils.closeQuietly(out);
        }
        try {
            InputStream in = Gdx.files.local("test.txt").read();
            StreamUtils.closeQuietly(in);
            message += "Open local success\n";
        } catch (Throwable e) {
            message += "Couldn't open localstorage/test.txt\n" + e.getMessage() + "\n";
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(Gdx.files.local("test.txt").read()));
            if (!in.readLine().equals("test"))
                message += "Read result wrong\n";
            else
                message += "Read local success\n";
        } catch (GdxRuntimeException ex) {
            message += "Couldn't open localstorage/test.txt\n";
        } catch (IOException e) {
            message += "Couldn't read localstorage/test.txt\n";
        } finally {
            StreamUtils.closeQuietly(in);
        }
        try {
            byte[] testBytes = Gdx.files.local("test.txt").readBytes();
            if (Arrays.equals("test".getBytes(), testBytes))
                message += "Read into byte array success\n";
            else
                fail();
        } catch (Throwable e) {
            message += "Couldn't read localstorage/test.txt\n" + e.getMessage() + "\n";
        }
        if (!Gdx.files.local("test.txt").delete())
            message += "Couldn't delete localstorage/test.txt";
    }
    try {
        testClasspath();
        testInternal();
        testExternal();
        testAbsolute();
        testLocal();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) BufferedWriter(java.io.BufferedWriter)

Example 73 with OutputStreamWriter

use of java.io.OutputStreamWriter in project bazel by bazelbuild.

the class SourceManifestAction method writeFile.

/**
   * Sort the entries in both the normal and root manifests and write the output
   * file.
   *
   * @param out is the message stream to write errors to.
   * @param output The actual mapping of the output manifest.
   * @throws IOException
   */
private void writeFile(OutputStream out, Map<PathFragment, Artifact> output) throws IOException {
    Writer manifestFile = new BufferedWriter(new OutputStreamWriter(out, ISO_8859_1));
    Comparator<Map.Entry<PathFragment, Artifact>> fragmentComparator = new Comparator<Map.Entry<PathFragment, Artifact>>() {

        @Override
        public int compare(Map.Entry<PathFragment, Artifact> path1, Map.Entry<PathFragment, Artifact> path2) {
            return path1.getKey().compareTo(path2.getKey());
        }
    };
    List<Map.Entry<PathFragment, Artifact>> sortedManifest = new ArrayList<>(output.entrySet());
    Collections.sort(sortedManifest, fragmentComparator);
    for (Map.Entry<PathFragment, Artifact> line : sortedManifest) {
        manifestWriter.writeEntry(manifestFile, line.getKey(), line.getValue());
    }
    manifestFile.flush();
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Artifact(com.google.devtools.build.lib.actions.Artifact) BufferedWriter(java.io.BufferedWriter) Comparator(java.util.Comparator)

Example 74 with OutputStreamWriter

use of java.io.OutputStreamWriter in project blade by biezhi.

the class StreamKit method writeText.

/** 将字符串写入到指定输出流中。 */
public static void writeText(CharSequence chars, OutputStream out, String charset, boolean closeOut) throws IOException {
    Writer writer = charset == null ? new OutputStreamWriter(out) : new OutputStreamWriter(out, charset);
    writeText(chars, writer, closeOut);
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 75 with OutputStreamWriter

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

the class TestPhrase method testExtract.

public void testExtract() throws Exception {
    List<File> fileList = FolderWalker.open(FOLDER);
    Map<String, String> phraseMap = new TreeMap<String, String>();
    int i = 0;
    for (File file : fileList) {
        System.out.print(++i + " / " + fileList.size() + " " + file.getName() + " ");
        String path = file.getAbsolutePath();
        List<String> phraseList = MutualInformationEntropyPhraseExtractor.extract(IOUtil.readTxt(path), 3);
        System.out.print(phraseList);
        for (String phrase : phraseList) {
            phraseMap.put(phrase, file.getAbsolutePath());
        }
        System.out.println();
    }
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data/phrase.txt")));
    for (Map.Entry<String, String> entry : phraseMap.entrySet()) {
        bw.write(entry.getKey() + "\t" + entry.getValue());
        bw.newLine();
    }
    bw.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

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