java.lang.System.setIn()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(278)

本文整理了Java中java.lang.System.setIn()方法的一些代码示例,展示了System.setIn()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。System.setIn()方法的具体详情如下:
包路径:java.lang.System
类名称:System
方法名:setIn

System.setIn介绍

[英]Sets the standard input stream to the given user defined input stream.
[中]将标准输入流设置为给定的用户定义输入流。

代码示例

代码示例来源:origin: fengjiachun/Jupiter

public static void setIn(InputStream in) {
  System.setIn(in);
}

代码示例来源:origin: fengjiachun/Jupiter

public static void setIn(InputStream in) {
  System.setIn(in);
}

代码示例来源:origin: stackoverflow.com

ByteArrayInputStream in = new ByteArrayInputStream("My string".getBytes());
System.setIn(in);

// do your thing

// optionally, reset System.in to its original
System.setIn(System.in)

代码示例来源:origin: stackoverflow.com

String data = "Hello, World!\r\n";
InputStream stdin = System.in;
try {
 System.setIn(new ByteArrayInputStream(data.getBytes()));
 Scanner scanner = new Scanner(System.in);
 System.out.println(scanner.nextLine());
} finally {
 System.setIn(stdin);
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
  */
 public static void main(String[] args) {
  LatticeXMLReader reader = new LatticeXMLReader();
  try {
   System.setIn(new FileInputStream(args[0]));
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  reader.load(System.in);

  int numLattices = 0;
  for(Lattice lattice : reader) {
   System.out.println(lattice.toString());
   numLattices++;
  }
  System.out.printf("\nLoaded %d lattices\n", numLattices);
 }
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws IOException {

  // 1. create the pipes
  PipedInputStream inPipe = new PipedInputStream();
  PipedInputStream outPipe = new PipedInputStream();

  // 2. set the System.in and System.out streams
  System.setIn(inPipe);
  System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));

  PrintWriter inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);

  // 3. create the gui 
  JFrame frame = new JFrame("\"Console\"");
  frame.add(console(outPipe, inWriter));
  frame.setSize(400, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);

  // 4. write some output (to JTextArea)
  System.out.println("Hello World!");
  System.out.println("Test");
  System.out.println("Test");
  System.out.println("Test");

  // 5. get some input (from JTextArea)
  Scanner s = new Scanner(System.in);
  System.out.printf("got from input: \"%s\"%n", s.nextLine());
}

代码示例来源:origin: apache/flink

protected static void resetStreamsAndSendOutput() {
  System.setOut(ORIGINAL_STDOUT);
  System.setErr(ORIGINAL_STDERR);
  System.setIn(ORIGINAL_STDIN);
  LOG.info("Sending stdout content through logger: \n\n{}\n\n", outContent.toString());
  LOG.info("Sending stderr content through logger: \n\n{}\n\n", errContent.toString());
}

代码示例来源:origin: jphp-group/jphp

@Signature
public static void setIn(Environment env, @Arg(typeClass = "php\\io\\Stream", nullable = true) Memory stream) {
  if (stream.isNull()) {
    System.setIn(null);
  } else {
    System.setIn(Stream.getInputStream(env, stream));
  }
}

代码示例来源:origin: spring-projects/spring-batch

@After
public void tearDown() throws Exception {
  System.setIn(stdin);
  StubJobLauncher.tearDown();
}

代码示例来源:origin: spring-projects/spring-batch

@Before
public void setUp() throws Exception {
  JobExecution jobExecution = new JobExecution(null, new Long(1), null, null);
  ExitStatus exitStatus = ExitStatus.COMPLETED;
  jobExecution.setExitStatus(exitStatus);
  StubJobLauncher.jobExecution = jobExecution;
  stdin = System.in;
  System.setIn(new InputStream() {
    @Override
    public int read() {
      return -1;
    }
  });
}

代码示例来源:origin: remkop/picocli

@Test
public void testInteractiveOptionReadsFromStdInMultiLinePrompt() {
  class App {
    @Option(names = "-x", description = {"Pwd%nline2", "ignored"}, interactive = true) int x;
    @Option(names = "-z") int z;
  }
  PrintStream out = System.out;
  InputStream in = System.in;
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    System.setOut(new PrintStream(baos));
    System.setIn(new ByteArrayInputStream("123".getBytes()));
    App app = new App();
    CommandLine cmd = new CommandLine(app);
    cmd.parse("-x", "-z", "987");
    String expectedPrompt = format("Enter value for -x (Pwd%nline2): ");
    assertEquals(expectedPrompt, baos.toString());
    assertEquals(123, app.x);
    assertEquals(987, app.z);
  } finally {
    System.setOut(out);
    System.setIn(in);
  }
}

代码示例来源:origin: remkop/picocli

@Test
public void testInteractivePositionalReadsFromStdIn() {
  class App {
    @Parameters(index = "0", description = {"Pwd%nline2", "ignored"}, interactive = true) int x;
    @Parameters(index = "1") int z;
  }
  PrintStream out = System.out;
  InputStream in = System.in;
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    System.setOut(new PrintStream(baos));
    System.setIn(new ByteArrayInputStream("123".getBytes()));
    App app = new App();
    CommandLine cmd = new CommandLine(app);
    cmd.parse("987");
    String expectedPrompt = format("Enter value for position 0 (Pwd%nline2): ");
    assertEquals(expectedPrompt, baos.toString());
    assertEquals(123, app.x);
    assertEquals(987, app.z);
  } finally {
    System.setOut(out);
    System.setIn(in);
  }
}

代码示例来源:origin: apache/flink

System.setIn(in);

代码示例来源:origin: remkop/picocli

@Test
public void testInteractivePositional2ReadsFromStdIn() {
  class App {
    @Parameters(index = "0") int a;
    @Parameters(index = "1", description = {"Pwd%nline2", "ignored"}, interactive = true) int x;
    @Parameters(index = "2") int z;
  }
  PrintStream out = System.out;
  InputStream in = System.in;
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    System.setOut(new PrintStream(baos));
    System.setIn(new ByteArrayInputStream("123".getBytes()));
    App app = new App();
    CommandLine cmd = new CommandLine(app);
    cmd.parse("333", "987");
    String expectedPrompt = format("Enter value for position 1 (Pwd%nline2): ");
    assertEquals(expectedPrompt, baos.toString());
    assertEquals(333, app.a);
    assertEquals(123, app.x);
    assertEquals(987, app.z);
  } finally {
    System.setOut(out);
    System.setIn(in);
  }
}

代码示例来源:origin: apache/flink

System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
System.setIn(in);

代码示例来源:origin: remkop/picocli

@Test
public void testInteractiveOptionReadsFromStdIn() {
  class App {
    @Option(names = "-x", description = {"Pwd", "line2"}, interactive = true) int x;
    @Option(names = "-z") int z;
  }
  PrintStream out = System.out;
  InputStream in = System.in;
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    System.setOut(new PrintStream(baos));
    System.setIn(new ByteArrayInputStream("123".getBytes()));
    App app = new App();
    CommandLine cmd = new CommandLine(app);
    cmd.parse("-x");
    assertEquals("Enter value for -x (Pwd): ", baos.toString());
    assertEquals(123, app.x);
    assertEquals(0, app.z);
    cmd.parse("-z", "678");
    assertEquals(0, app.x);
    assertEquals(678, app.z);
  } finally {
    System.setOut(out);
    System.setIn(in);
  }
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testWithStdinCommandLine() throws Throwable {
  System.setIn(new InputStream() {
    char[] input = (jobPath+"\n"+jobName+"\nfoo=bar\nspam=bucket").toCharArray();
    int index = 0;
    @Override
    public int available() {
      return input.length - index;
    }
    @Override
    public int read() {
      return index<input.length-1 ? (int) input[index++] : -1;
    }
  });
  CommandLineJobRunner.main(new String[0]);
  assertEquals(0, StubSystemExiter.status);
  assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testWithStdinCommandLineWithEmptyLines() throws Throwable {
  System.setIn(new InputStream() {
    char[] input = (jobPath+"\n"+jobName+"\nfoo=bar\n\nspam=bucket\n\n").toCharArray();
    int index = 0;
    @Override
    public int available() {
      return input.length - index;
    }
    @Override
    public int read() {
      return index<input.length-1 ? (int) input[index++] : -1;
    }
  });
  CommandLineJobRunner.main(new String[0]);
  assertEquals(0, StubSystemExiter.status);
  assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testWithStdinParameters() throws Throwable {
  String[] args = new String[] { jobPath, jobName };
  System.setIn(new InputStream() {
    char[] input = ("foo=bar\nspam=bucket").toCharArray();
    int index = 0;
    @Override
    public int available() {
      return input.length - index;
    }
    @Override
    public int read() {
      return index<input.length-1 ? (int) input[index++] : -1;
    }
  });
  CommandLineJobRunner.main(args);
  assertEquals(0, StubSystemExiter.status);
  assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testWithInvalidStdin() throws Throwable {
  System.setIn(new InputStream() {
    @Override
    public int available() throws IOException {
      throw new IOException("Planned");
    }
    @Override
    public int read() {
      return -1;
    }
  });
  CommandLineJobRunner.main(new String[] { jobPath, jobName });
  assertEquals(0, StubSystemExiter.status);
  assertEquals(0, StubJobLauncher.jobParameters.getParameters().size());
}

相关文章