Skip to content

Commit 33ce4db

Browse files
Example using inputs from a genrule
1 parent 5698dd2 commit 33ce4db

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

java/com/engflow/internship/setinput/README.md

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
NUM_FILES = 10
2+
3+
[genrule(
4+
name = "benchmark_test_file" + str(x),
5+
outs = ["benchmark_test_file" + str(x) + ".txt"],
6+
cmd_bash = "echo 'benchmark test file " + str(x) + "' > $@",
7+
) for x in range(1,NUM_FILES+1)]
8+
9+
filegroup(
10+
name = "genfile",
11+
srcs = glob(["genfile/benchmark_test_file*.txt"]),
12+
visibility = ["//visibility:public"],
13+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
java_library(
2+
name = "genreader",
3+
srcs = ["GenReader.java"],
4+
visibility = ["//visibility:public"],
5+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.engflow.internship.setinput.genreader;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.io.BufferedReader;
7+
8+
public class GenReader {
9+
10+
/**
11+
* Reads the content of all files in the specified directory and prints it to the console.
12+
* @param inputPath
13+
*/
14+
public void readFiles(String inputPath) {
15+
File directory = new File(inputPath);
16+
17+
// Check if the directory exists and is a directory
18+
if (!directory.exists() || !directory.isDirectory()) {
19+
System.out.println("Invalid directory path: " + inputPath);
20+
return;
21+
}
22+
23+
// List all files in the directory and check if there are any
24+
File[] files = directory.listFiles();
25+
if (files == null || files.length == 0) {
26+
System.out.println("No files found in the directory: " + inputPath);
27+
return;
28+
}
29+
30+
// Read the content of each file and print it to the console
31+
for (File file : files) {
32+
if (file.isFile()) {
33+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
34+
String line;
35+
System.out.println("Reading file: " + file.getName());
36+
while ((line = reader.readLine()) != null) {
37+
System.out.println(line);
38+
}
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
}
45+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_java//java:defs.bzl", "java_binary")
2+
3+
java_binary(
4+
name = "main",
5+
srcs = ["Main.java"],
6+
deps = [
7+
"//java/com/engflow/internship/setinput/genreader",
8+
],
9+
data = [
10+
"//java/com/engflow/internship/setinput/genfile:genfile",
11+
],
12+
main_class = "com.engflow.internship.setinput.main.Main",
13+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.engflow.internship.setinput.main;
2+
3+
import com.engflow.internship.setinput.genreader.GenReader;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
GenReader reader = new GenReader();
9+
10+
String filePath = "//java/com/engflow/internship/setinput/genfile:genfile";
11+
12+
reader.readFiles(filePath);
13+
}
14+
}

0 commit comments

Comments
 (0)