Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:SerialVersionUID.java

/**
 * Generate a mapping of the serial version UIDs for the serializable classes
 * under the jboss dist directory//from  ww  w  .  jav  a  2s.c o  m
 * 
 * @param args -
 *          [0] = jboss dist root directory
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Usage: jboss-home | -rihome ri-home");
        System.exit(1);
    }
    File distHome = new File(args[0]);
    Map classVersionMap = null;
    if (args.length == 2)
        classVersionMap = generateRISerialVersionUIDReport(distHome);
    else
        classVersionMap = generateJBossSerialVersionUIDReport(distHome);
    // Write the map out the object file
    log.info("Total classes with serialVersionUID != 0: " + classVersionMap.size());
    FileOutputStream fos = new FileOutputStream("serialuid.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(classVersionMap);
    fos.close();
}

From source file:importer.handler.post.stages.Splitter.java

/** test and commandline utility */
public static void main(String[] args) {
    if (args.length >= 1) {
        try {//from  ww w  .ja v a  2  s  . c o  m
            int i = 0;
            int fileIndex = 0;
            // see if the user supplied a conf file
            String textConf = Discriminator.defaultConf;
            while (i < args.length) {
                if (args[i].equals("-c") && i < args.length - 1) {
                    textConf = readConfig(args[i + 1]);
                    i += 2;
                } else {
                    fileIndex = i;
                    i++;
                }
            }
            File f = new File(args[fileIndex]);
            char[] data = new char[(int) f.length()];
            FileReader fr = new FileReader(f);
            fr.read(data);
            JSONObject config = (JSONObject) JSONValue.parse(textConf);
            Splitter split = new Splitter(config);
            Map<String, String> map = split.split(new String(data));
            Set<String> keys = map.keySet();
            String rawFileName = args[fileIndex];
            int pos = rawFileName.lastIndexOf(".");
            if (pos != -1)
                rawFileName = rawFileName.substring(0, pos);
            Iterator<String> iter = keys.iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                String fName = rawFileName + "-" + key + ".xml";
                File g = new File(fName);
                if (g.exists())
                    g.delete();
                FileOutputStream fos = new FileOutputStream(g);
                fos.write(map.get(key).getBytes("UTF-8"));
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    } else
        System.out.println("usage: java -jar split.jar [-c json-config] <tei-xml>\n");
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);/*from   w  ww .  j ava 2  s. co  m*/

    while (!def.needsInput()) {
        int numCompressedBytes = def.deflate(output, 0, output.length);
        if (numCompressedBytes > 0) {
            fout.write(output, 0, numCompressedBytes);
        }
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
}

From source file:MainClass.java

public static void main(String[] args) {
    String phrase = new String("text\n");
    String dirname = "C:/test"; // Directory name
    String filename = "byteData.txt";
    File aFile = new File(dirname, filename);
    // Create the file output stream
    FileOutputStream file = null;
    try {/* www.  ja  va  2  s .  c om*/
        file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();
    buf.put(bytes);
    buf.flip();
    try {
        outChannel.write(buf);
        file.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    String phrase = new String("www.java2s.com API \n");
    File aFile = new File("test.txt");

    FileOutputStream file = null;
    try {// ww w .  j a  v a2 s .  c o  m
        file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();
    buf.put(bytes);
    buf.flip();

    try {
        outChannel.write(buf);
        file.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    try {/*w w  w  .  j  a v a 2s .  c o m*/

        FileOutputStream fos = new FileOutputStream(args[0]);

        DataOutputStream dos = new DataOutputStream(fos);

        dos.writeBoolean(false);
        dos.writeByte(Byte.MAX_VALUE);
        dos.writeChar('A');
        dos.writeDouble(Double.MAX_VALUE);
        dos.writeFloat(Float.MAX_VALUE);
        dos.writeInt(Integer.MAX_VALUE);
        dos.writeLong(Long.MAX_VALUE);
        dos.writeShort(Short.MAX_VALUE);

        fos.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:com.linkedin.restli.tools.data.FilterSchemaGenerator.java

public static void main(String[] args) {
    CommandLine cl = null;/*from   ww  w  . j  a va  2  s  .  co  m*/
    try {
        final CommandLineParser parser = new GnuParser();
        cl = parser.parse(_options, args);
    } catch (ParseException e) {
        _log.error("Invalid arguments: " + e.getMessage());
        reportInvalidArguments();
    }

    final String[] directoryArgs = cl.getArgs();
    if (directoryArgs.length != 2) {
        reportInvalidArguments();
    }

    final File sourceDirectory = new File(directoryArgs[0]);
    if (!sourceDirectory.exists()) {
        _log.error(sourceDirectory.getPath() + " does not exist");
        System.exit(1);
    }
    if (!sourceDirectory.isDirectory()) {
        _log.error(sourceDirectory.getPath() + " is not a directory");
        System.exit(1);
    }
    final URI sourceDirectoryURI = sourceDirectory.toURI();

    final File outputDirectory = new File(directoryArgs[1]);
    if (outputDirectory.exists() && !sourceDirectory.isDirectory()) {
        _log.error(outputDirectory.getPath() + " is not a directory");
        System.exit(1);
    }

    final boolean isAvroMode = cl.hasOption('a');
    final String predicateExpression = cl.getOptionValue('e');
    final Predicate predicate = PredicateExpressionParser.parse(predicateExpression);

    final Collection<File> sourceFiles = FileUtil.listFiles(sourceDirectory, null);
    int exitCode = 0;
    for (File sourceFile : sourceFiles) {
        try {
            final ValidationOptions val = new ValidationOptions();
            val.setAvroUnionMode(isAvroMode);

            final SchemaParser schemaParser = new SchemaParser();
            schemaParser.setValidationOptions(val);

            schemaParser.parse(new FileInputStream(sourceFile));
            if (schemaParser.hasError()) {
                _log.error("Error parsing " + sourceFile.getPath() + ": " + schemaParser.errorMessageBuilder());
                exitCode = 1;
                continue;
            }

            final DataSchema originalSchema = schemaParser.topLevelDataSchemas().get(0);
            if (!(originalSchema instanceof NamedDataSchema)) {
                _log.error(sourceFile.getPath() + " does not contain valid NamedDataSchema");
                exitCode = 1;
                continue;
            }

            final SchemaParser filterParser = new SchemaParser();
            filterParser.setValidationOptions(val);

            final NamedDataSchema filteredSchema = Filters.removeByPredicate((NamedDataSchema) originalSchema,
                    predicate, filterParser);
            if (filterParser.hasError()) {
                _log.error("Error applying predicate: " + filterParser.errorMessageBuilder());
                exitCode = 1;
                continue;
            }

            final String relativePath = sourceDirectoryURI.relativize(sourceFile.toURI()).getPath();
            final String outputFilePath = outputDirectory.getPath() + File.separator + relativePath;
            final File outputFile = new File(outputFilePath);
            final File outputFileParent = outputFile.getParentFile();
            outputFileParent.mkdirs();
            if (!outputFileParent.exists()) {
                _log.error("Unable to write filtered schema to " + outputFileParent.getPath());
                exitCode = 1;
                continue;
            }

            FileOutputStream fout = new FileOutputStream(outputFile);
            String schemaJson = SchemaToJsonEncoder.schemaToJson(filteredSchema, JsonBuilder.Pretty.INDENTED);
            fout.write(schemaJson.getBytes(RestConstants.DEFAULT_CHARSET));
            fout.close();
        } catch (IOException e) {
            _log.error(e.getMessage());
            exitCode = 1;
        }
    }

    System.exit(exitCode);
}

From source file:MainClass.java

public static void main(String args[]) {
    FileOutputStream fileOutputStream;
    FileChannel fileChannel;//from www . j  av a  2s  . com
    ByteBuffer byteBuffer;

    try {
        fileOutputStream = new FileOutputStream("test.txt");
        fileChannel = fileOutputStream.getChannel();
        byteBuffer = ByteBuffer.allocateDirect(26);

        for (int i = 0; i < 26; i++)
            byteBuffer.put((byte) ('A' + i));
        byteBuffer.rewind();
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

From source file:com.inkubator.common.util.NewMain.java

/**
 * @param args the command line arguments
 *///from w  ww .  jav  a  2  s . com
public static void main(String[] args) throws IOException {

    File file1 = new File(
            "C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\JSON_Ek\\Surabaya\\Page1.txt");
    File file2 = new File(
            "C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\JSON_Ek\\Surabaya\\Page2.txt");
    //        File file3 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\json\\json\\menado\\page3.txt");
    File file3 = new File(
            "C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\JSON_Ek\\Surabaya\\Page3.txt");
    File file4 = new File(
            "C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\JSON_Ek\\Surabaya\\Page4.txt");
    File file5 = new File(
            "C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\JSON_Ek\\Surabaya\\Page5.txt");
    File file6 = new File(
            "C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\JSON_Ek\\Surabaya\\Page6.txt");
    //        File file7 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 7.txt");
    //        File file8 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 8.txt");
    //        File file9 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 9.txt");
    //        File file10 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 10.txt");
    //        File file11 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 11.txt");
    //        File file12 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 12.txt");
    //        File file13 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 13.txt");
    //        File file14 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 14.txt");
    //        File file15 = new File("C:\\Users\\deni.fahri\\AppData\\Roaming\\Skype\\My Skype Received Files\\Bandung\\Bandung\\Page 15.txt");
    //        File file16 = new File("C:\\Users\\deni.fahri\\Downloads\\page16.txt");

    //        File file2 = new File("C:\\Users\\deni.fahri\\Documents\\hasil.txt");
    String agoda = FilesUtil.getAsStringFromFile(file1);
    String agoda1 = FilesUtil.getAsStringFromFile(file2);
    String agoda2 = FilesUtil.getAsStringFromFile(file3);
    String agoda3 = FilesUtil.getAsStringFromFile(file4);
    String agoda4 = FilesUtil.getAsStringFromFile(file5);
    String agoda5 = FilesUtil.getAsStringFromFile(file6);
    //        String agoda6 = FilesUtil.getAsStringFromFile(file7);
    //        String agoda7 = FilesUtil.getAsStringFromFile(file8);
    //        String agoda8 = FilesUtil.getAsStringFromFile(file9);
    //        String agoda9 = FilesUtil.getAsStringFromFile(file10);
    //        String agoda10 = FilesUtil.getAsStringFromFile(file11);
    //        String agoda11 = FilesUtil.getAsStringFromFile(file12);
    //        String agoda12 = FilesUtil.getAsStringFromFile(file13);
    //        String agoda13 = FilesUtil.getAsStringFromFile(file14);
    //        String agoda14 = FilesUtil.getAsStringFromFile(file15);
    //        String agoda15 = FilesUtil.getAsStringFromFile(file16);
    ////        System.out.println(" Test Nya adalah :" + agoda);
    ////        String a=StringUtils.substringAfter("\"HotelTranslatedName\":", agoda);
    ////        System.out.println(" hasil; "+a);
    ////        // TODO code application logic here
    ////        System.out.println("Nilai " + JsonConverter.getValueByKeyStatic(agoda, "HotelTranslatedName"));
    TypeToken<List<HotelModel>> token = new TypeToken<List<HotelModel>>() {
    };
    Gson gson = new GsonBuilder().create();
    //        List<HotelModel> data = new ArrayList<>();
    //        HotelModel hotelModel = new HotelModel();
    //        hotelModel.setAddress("sdfsdffsfsdfsdfdsfdsf");
    //        hotelModel.setAccommodationName("Aku");
    //        HotelModel hotelModel1 = new HotelModel();
    //        hotelModel1.setAddress("sdfsdffsfsdfsdfdsfdsf");
    //        hotelModel1.setAccommodationName("Avvvku");
    //        HotelModel hotelModel2 = new HotelModel();
    //        hotelModel2.setAddress("sdfsdffsfsdfsdfdsfdsf");
    //        hotelModel2.setAccommodationName("Akvvvu");
    //        data.add(hotelModel);
    //        data.add(hotelModel1);
    //        data.add(hotelModel2);
    //        String json = gson.toJson(data);
    List<HotelModel> total = new ArrayList<>();
    List<HotelModel> data1 = new ArrayList<>();
    List<HotelModel> data2 = new ArrayList<>();
    List<HotelModel> data3 = new ArrayList<>();
    List<HotelModel> data4 = new ArrayList<>();
    List<HotelModel> data5 = new ArrayList<>();
    List<HotelModel> data6 = new ArrayList<>();
    List<HotelModel> data7 = new ArrayList<>();
    List<HotelModel> data8 = new ArrayList<>();
    List<HotelModel> data9 = new ArrayList<>();
    List<HotelModel> data10 = new ArrayList<>();
    List<HotelModel> data11 = new ArrayList<>();
    List<HotelModel> data12 = new ArrayList<>();
    List<HotelModel> data13 = new ArrayList<>();
    List<HotelModel> data14 = new ArrayList<>();
    List<HotelModel> data15 = new ArrayList<>();
    List<HotelModel> data16 = new ArrayList<>();

    data1 = gson.fromJson(agoda, token.getType());
    data2 = gson.fromJson(agoda1, token.getType());
    data3 = gson.fromJson(agoda2, token.getType());
    data4 = gson.fromJson(agoda3, token.getType());
    data5 = gson.fromJson(agoda4, token.getType());
    data6 = gson.fromJson(agoda5, token.getType());
    //        data7 = gson.fromJson(agoda6, token.getType());
    //        data8 = gson.fromJson(agoda7, token.getType());
    //        data9 = gson.fromJson(agoda8, token.getType());
    //        data10 = gson.fromJson(agoda9, token.getType());
    //        data11 = gson.fromJson(agoda10, token.getType());
    //        data12 = gson.fromJson(agoda11, token.getType());
    //        data13 = gson.fromJson(agoda12, token.getType());
    //        data14 = gson.fromJson(agoda13, token.getType());
    //        data15 = gson.fromJson(agoda14, token.getType());
    //        data16 = gson.fromJson(agoda15, token.getType());
    total.addAll(data1);
    total.addAll(data2);
    total.addAll(data3);
    total.addAll(data4);
    total.addAll(data5);
    total.addAll(data6);
    //        total.addAll(data7);
    //        total.addAll(data8);
    //        total.addAll(data9);
    //        total.addAll(data10);
    //        total.addAll(data11);
    //        total.addAll(data12);
    //        total.addAll(data13);
    //        total.addAll(data14);
    //        total.addAll(data15);
    //        total.addAll(data16);
    System.out.println(" Ukurannn nya " + total.size());

    //        System.out.println(" Ukurannya " + data2.size());
    for (HotelModel mode : total) {
        System.out.println(mode);
    }
    //        HotelModel hotelModel = gson.fromJson(agoda, HotelModel.class);
    //        String Data = hotelModel.getHotelTranslatedName() + ";" + hotelModel.getStarRating() + ";" + hotelModel.getAddress() + ";" + hotelModel.getIsFreeWifi();
    //        FilesUtil.writeToFileFromString(file2, Data);
    //        System.out.println(hotelModel);
    //
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Agoda Data Hotel Surabaya");

    ////
    TreeMap<String, Object[]> datatoExel = new TreeMap<>();
    int i = 1;
    //        datatoExel.put("1", new Object[]{"Hotel Agoda Jakarta"});
    datatoExel.put("1", new Object[] { "Nama Hotel", "Arena", "Alamat", "Rating", "Apakah Gratis Wifi",
            "Harga Mulai Dari", "Longitude", "Latitude" });
    for (HotelModel mode : total) {
        datatoExel.put(String.valueOf(i + 1),
                new Object[] { mode.getHotelTranslatedName(), mode.getAreaName(), mode.getAddress(),
                        mode.getStarRating(), mode.getIsFreeWifi(),
                        mode.getTextPrice() + " " + mode.getCurrencyCode(), mode.getCoordinate().getLongitude(),
                        mode.getCoordinate().getLatitude() });
        i++;
    }
    //
    ////          int i=1;
    ////        for (HotelModel mode : data2) {
    ////             datatoExel.put(String.valueOf(i), new Object[]{1d, "John", 1500000d});
    //////        }
    ////       
    ////        datatoExel.put("4", new Object[]{3d, "Dean", 700000d});
    ////
    Set<String> keyset = datatoExel.keySet();
    int rownum = 0;
    for (String key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = datatoExel.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof Date) {
                cell.setCellValue((Date) obj);
            } else if (obj instanceof Boolean) {
                cell.setCellValue((Boolean) obj);
            } else if (obj instanceof String) {
                cell.setCellValue((String) obj);
            } else if (obj instanceof Double) {
                cell.setCellValue((Double) obj);
            }
        }
    }

    try {
        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\deni.fahri\\Documents\\Surabaya.xls"));
        workbook.write(out);
        out.close();
        System.out.println("Excel written successfully..");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:NIOCopy.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn;/*  ww  w .  ja  va2s.  co  m*/
    FileOutputStream fOut;
    FileChannel fIChan, fOChan;
    long fSize;
    MappedByteBuffer mBuf;

    fIn = new FileInputStream(args[0]);
    fOut = new FileOutputStream(args[1]);

    fIChan = fIn.getChannel();
    fOChan = fOut.getChannel();
    fSize = fIChan.size();

    mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
    fOChan.write(mBuf); // this copies the file
    fIChan.close();
    fIn.close();

    fOChan.close();
    fOut.close();
}