-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of basic parts of PrintStream
- Loading branch information
1 parent
50d7a64
commit 3c5539c
Showing
7 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
mod utils; | ||
use std::fs; | ||
use vm::vm::VM; | ||
|
||
#[test] | ||
fn should_write_file_with_print_stream() { | ||
let file_path = "../tmp/print_stream_test.txt"; | ||
VM::run("samples.io.printstreamexample.PrintStreamExample").unwrap(); | ||
|
||
assert!(fs::metadata(file_path).is_ok(), "File does not exist"); | ||
let content = fs::read_to_string(file_path).expect("Failed to read file"); | ||
assert_eq!( | ||
content, | ||
r#"Hello, PrintStream! | ||
First Line | ||
Second Line | ||
Third Line | ||
Hello as raw bytes | ||
This is written immediately. This follows after flush. | ||
This is an example of chaining PrintStreams. | ||
"#, | ||
"File content does not match" | ||
); | ||
fs::remove_file(file_path).expect("Failed to delete file"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package samples.io.printstreamexample; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
public class PrintStreamExample { | ||
private static final String FILE_NAME = "../tmp/print_stream_test.txt"; | ||
private static final boolean APPEND = true; | ||
|
||
public static void main(String[] args) throws IOException { | ||
try (PrintStream ps = new PrintStream(FILE_NAME)) { | ||
ps.println("Hello, PrintStream!"); | ||
} | ||
|
||
try (PrintStream ps = new PrintStream(new FileOutputStream(FILE_NAME, APPEND))) { | ||
ps.println("First Line"); | ||
ps.println("Second Line"); | ||
ps.println("Third Line"); | ||
} | ||
|
||
// https://github.com/hextriclosan/rusty-jvm/issues/127 | ||
// try (PrintStream ps = new PrintStream(new FileOutputStream(FILE_NAME, APPEND))) { | ||
// ps.printf("Hello %s, you are %d years old.%n", "Alice", 30); | ||
// } | ||
|
||
// try (PrintStream ps = new PrintStream(new FileOutputStream(FILE_NAME, APPEND))) { | ||
// System.setOut(ps); // Redirects standard output to the file | ||
// System.out.println("This will go to the file instead of the console."); | ||
// } | ||
|
||
try (PrintStream ps = new PrintStream(new FileOutputStream(FILE_NAME, APPEND))) { | ||
ps.write("Hello as raw bytes".getBytes()); // Writes raw bytes | ||
ps.println(); | ||
} | ||
|
||
// try (PrintStream ps = new PrintStream(new FileOutputStream(FILE_NAME, APPEND))) { | ||
// ps.println(new Person("John", 25)); | ||
// } | ||
|
||
try (PrintStream ps = new PrintStream(new FileOutputStream(FILE_NAME, APPEND))) { | ||
ps.print("This is written immediately. "); | ||
ps.flush(); // Ensures the data is written to the file | ||
ps.println("This follows after flush."); | ||
} | ||
|
||
try (PrintStream ps = new PrintStream(new PrintStream(new FileOutputStream(FILE_NAME, APPEND)))) { | ||
ps.println("This is an example of chaining PrintStreams."); | ||
} | ||
} | ||
} | ||
|
||
class Person { | ||
private final String name; | ||
private final int age; | ||
|
||
public Person(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Person{name='%s', age=%d}", name, age); | ||
} | ||
} |
Binary file not shown.
Binary file added
BIN
+1.75 KB
tests/test_data/samples/io/printstreamexample/PrintStreamExample.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters