Search in sources :

Example 46 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32Test method testGetPrivateProfileSection.

public final void testGetPrivateProfileSection() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSection", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[X]");
        writer.println("A=1");
        writer.println("B=X");
    } finally {
        writer.close();
    }
    final char[] buffer = new char[9];
    final DWORD len = Kernel32.INSTANCE.GetPrivateProfileSection("X", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(len.intValue(), 7);
    assertEquals(new String(buffer), "A=1\0B=X\0\0");
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 47 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32UtilTest method testWritePrivateProfileSection.

public final void testWritePrivateProfileSection() throws IOException {
    final File tmp = File.createTempFile("testWritePrivateProfileSecion", "ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[S1]");
        writer.println("A=1");
        writer.println("B=X");
        writer.println("[S2]");
        writer.println("C=2");
        writer.println("foo=bar");
    } finally {
        writer.close();
    }
    Kernel32Util.writePrivateProfileSection("S1", new String[] { "A=3", "E=Z" }, tmp.getCanonicalPath());
    final BufferedReader reader = new BufferedReader(new FileReader(tmp));
    try {
        assertEquals(reader.readLine(), "[S1]");
        assertEquals(reader.readLine(), "A=3");
        assertEquals(reader.readLine(), "E=Z");
        assertEquals(reader.readLine(), "[S2]");
        assertEquals(reader.readLine(), "C=2");
        assertEquals(reader.readLine(), "foo=bar");
    } finally {
        reader.close();
    }
}
Also used : FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 48 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32Test method testWritePrivateProfileSection.

public final void testWritePrivateProfileSection() throws IOException {
    final File tmp = File.createTempFile("testWritePrivateProfileSection", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[S1]");
        writer.println("A=1");
        writer.println("B=X");
    } finally {
        writer.close();
    }
    final boolean result = Kernel32.INSTANCE.WritePrivateProfileSection("S1", "A=3\0E=Z\0\0", tmp.getCanonicalPath());
    assertTrue(result);
    final BufferedReader reader = new BufferedReader(new FileReader(tmp));
    assertEquals(reader.readLine(), "[S1]");
    assertTrue(reader.readLine().matches("A\\s*=\\s*3"));
    assertTrue(reader.readLine().matches("E\\s*=\\s*Z"));
    reader.close();
}
Also used : FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 49 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32Test method testWritePrivateProfileString.

public final void testWritePrivateProfileString() throws IOException {
    final File tmp = File.createTempFile("testWritePrivateProfileString", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    writer.println("[Section]");
    writer.println("existingKey = ABC");
    writer.println("removedKey = JKL");
    writer.close();
    assertTrue(Kernel32.INSTANCE.WritePrivateProfileString("Section", "existingKey", "DEF", tmp.getCanonicalPath()));
    assertTrue(Kernel32.INSTANCE.WritePrivateProfileString("Section", "addedKey", "GHI", tmp.getCanonicalPath()));
    assertTrue(Kernel32.INSTANCE.WritePrivateProfileString("Section", "removedKey", null, tmp.getCanonicalPath()));
    final BufferedReader reader = new BufferedReader(new FileReader(tmp));
    assertEquals(reader.readLine(), "[Section]");
    assertTrue(reader.readLine().matches("existingKey\\s*=\\s*DEF"));
    assertTrue(reader.readLine().matches("addedKey\\s*=\\s*GHI"));
    assertEquals(reader.readLine(), null);
    reader.close();
}
Also used : FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 50 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32Test method testGetPrivateProfileSectionNames.

public final void testGetPrivateProfileSectionNames() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSectionNames", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[S1]");
        writer.println("[S2]");
    } finally {
        writer.close();
    }
    final char[] buffer = new char[7];
    final DWORD len = Kernel32.INSTANCE.GetPrivateProfileSectionNames(buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(len.intValue(), 5);
    assertEquals(new String(buffer), "S1\0S2\0\0");
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)4214 FileWriter (java.io.FileWriter)2181 File (java.io.File)1879 IOException (java.io.IOException)1847 OutputStreamWriter (java.io.OutputStreamWriter)1344 BufferedReader (java.io.BufferedReader)747 FileOutputStream (java.io.FileOutputStream)656 ArrayList (java.util.ArrayList)386 FileReader (java.io.FileReader)376 InputStreamReader (java.io.InputStreamReader)349 PrintWriter (java.io.PrintWriter)324 Writer (java.io.Writer)324 Test (org.junit.Test)286 FileNotFoundException (java.io.FileNotFoundException)217 OutputStream (java.io.OutputStream)213 HashMap (java.util.HashMap)200 Path (java.nio.file.Path)177 InputStream (java.io.InputStream)171 FileInputStream (java.io.FileInputStream)158 StringWriter (java.io.StringWriter)143