Java Text File Write writeString(String path, String key, String value)

Here you can find the source of writeString(String path, String key, String value)

Description

Writes a registry key to the Windows registry (only string type, REG_SZ).

License

Open Source License

Exception

Parameter Description
InterruptedException an exception
IOException an exception

Declaration

public static final void writeString(String path, String key, String value) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w ww  .  j av a2 s  .  c  o m*/
 * Syncany, www.syncany.org
 * Copyright (C) 2011-2015 Philipp C. Heckel <philipp.heckel@gmail.com> 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Main {
    /**
     * Writes a registry key to the Windows registry (only string type, REG_SZ).
     * 
     * <p>This method executes the following command (example):
     * <pre>
     * $> reg add /f HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v Syncany /t REG_SZ /d "<path>"
     * </pre>
     * 
     * @throws InterruptedException 
     * @throws IOException 
     */
    public static final void writeString(String path, String key, String value) throws IOException {
        Objects.requireNonNull(path, value);

        // Build command
        List<String> command = new ArrayList<>();
        command.add("reg");
        command.add("add");
        command.add(path);

        if (key != null) {
            command.add("/v");
            command.add(key);
        }

        command.add("/t");
        command.add("REG_SZ");
        command.add("/d");
        command.add(value);
        command.add("/f");

        // And run it
        try {
            Process regProcess = Runtime.getRuntime().exec(command.toArray(new String[0]));

            throwAwayStream(regProcess.getInputStream());
            throwAwayStream(regProcess.getErrorStream());

            regProcess.waitFor();
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
    }

    private static void throwAwayStream(InputStream inputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        while ((reader.readLine()) != null) {
            // Do nothing.
        }
    }
}

Related

  1. writeString(String data, String file_path)
  2. writeString(String fileName, String str)
  3. writeString(String filePath, boolean append, String content, String encoding)
  4. writeString(String filePath, String content)
  5. writeString(String par0)
  6. writeString(String path, String str)
  7. writeString(String path, String str, boolean append)
  8. writeString(String s, File output, String encoding)
  9. writeString(String string, ObjectOutput out)