Java file input as command line argument

How do you process information in Java that was input from a file. For Example: suppose you have a file input.txt. The contents of this file is: abcdefghizzzzjklmnop azzbcdefghijklmnop

My hope would be that the information would be put into the argument array of strings such that the following code would output "abcdefghizzzzjklmnop"

class Test {
    public static void main(String[] args) {

The command I have been using throws an array out of bound exception. This command is:

java Test < input.txt

Non-file based arguments work fine though. ie. java Test hello,a nd java Test < input.txt hello.

More information: I have tried putting the file contents all on one line to see if \n \r characters may be messing things up. That didn't seem to help.

Also, I can't use the bufferedreader class for this because this is for a program for school, and it has to work with my professors shell script. He went over this during class, but I didn't write it down (or I can't find it).

Any help?

Asked by: Miller239 | Posted: 23-01-2022

Answer 1

You should be able to read the input data from

Here's some quick-and-dirty example code. javac; java Test <

class Test
    public static void main (String[] args)
        byte[] bytes = new byte[1024];
            while ( > 0)
                int read = (bytes, 0, 1024);
                System.out.write (bytes, 0, read);
        } catch (Exception e)
            e.printStackTrace ();

Answered by: Freddie333 | Posted: 24-02-2022

Answer 2

It seems any time you post a formal question about your problem, you figure it out.

Inputing a file via "< input.txt" inputs it as user input rather than as a command line argument. I realized this shortly after I explained why the bufferedreader class wouldn't work.

Turns out you have to use the buffered reader class.

Answered by: Rubie279 | Posted: 24-02-2022

Answer 3

I'm not sure why you want to pass the contents of a file as command line arguments unless you're doing some weird testbed.

You could write a script that would read your file, generate a temporary script in which the java command is followed by your needs.

Answered by: Kate447 | Posted: 24-02-2022

Similar questions

java - How to get input from command line argument

currently my file is run by me calling Java Server and then entering a port number after the program has started like this: java Server Port #: I do this by creating a scanner in my main and just printing the prompt and waiting for input. and I can enter 7777 to set the port to 7777. but how do I make it so I can just enter the port number like this?

java - The issue of * in Command line argument

I wrote a program in Java that accepts input via command line arguments. I get an input of two numbers and an operator from the command line. To multiply two numbers, I have to give input as e.g. 5 3 *, but it's not working as written. Why is it not accepting * from the command line?

java - Junit -- Command line argument

I have just started learning JUnit. I have code as follows: public class MyClass { private void verify(args) {...} private void process(clientoptions) {...} public static void main(String[] args) { verify(args); //get client and do something ..... // some more code here.... ........ // and then process(clientoptions); } } How do you write a test in Jun...

c# - What is specifically a Command Line Argument?

I always wanted to ask my college instructor what does it do specifically in public static void main(String[] args) but I never actually had a chance because it was last term, I never actually what does it do and what its importance, we just use because of practice. but as I try to learn C# these type of question arises, (I am trying to learn C# from the MSDN tutorials but the definition of command line argume...

java - I wanted to enter a command line argument for my string

public class NewClass1 { public static void main(String[] args) throws FileNotFoundException { String datasetFile = args[0]; BufferedReader in = new BufferedReader(new FileReader(datasetFile)); } } It generated the following error Exception in thread "main" abc (The system cannot find the file specified) at

How to supply command line argument to perl script through Java

I am running perl script through Java. The code is as shown below. try { Process p = Runtime.getRuntime().exec("perl"); BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream())); System.out.println(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } My perl scrip...

Command Line Argument as java File

I am trying to pass a java file as command line arg in Eclipse. But, each and every time , it's throwing FileNotFoundException and showing Error - System cannot find the file specified). I have this Product file in same package as my main java file. One more point to mention, I am using FileReader. BufferedReader in my program to read that file. Is there anything ...

java - how to check if command line argument is given or not?

Closed. This question does not meet Stack Overflow guid...

why main method works without command line argument in java?

Closed. This question needs details or clarity. It ...

Java - How to parse large int as command line argument?

I need to write a short program that works for all values of n. n is a command line argument(args[0]). The problem is Integer.parseInt doesn't work for large values such as 20000000000. What could i do to get around this problem? The program is designed to print all values that are the power of 2 until that value is >= n and n has to be argument[0]. public class PowerOfTwo{ public static void main(String[] ...

Java Socket Command Line Argument

I have created a simple server and client in java that sends and receives messages from one another. At the moment I have the port number hard coded into the code but i would like to change that to user input from the command line. In the code below I am using args[0] for the port number but when I compile it fails, can anyone help? I am new to both java and sockets so sorry if this is a trivial question im...

java - Is it necessary for setter methods to have one argument?

Is it necessary for setter methods to have one argument? Usually setter methods accept one argument as the value of a certain property of an Object. What if I want to test first the validity which depends on another argument which is a boolean, if true, validate first, else just set the value. I am getting the values from clients through ftp server. Sometimes those files contain garbage values. For instance, a phon...

Generic method in Java without generic argument

In C# I can do actually this: //This is C# static T SomeMethod&lt;T&gt;() where T:new() { Console.WriteLine("Typeof T: "+typeof(T)); return new T(); } //And call the method here SomeMethod&lt;SomeClassName&gt;(); But for some reason I can't get it to work in Java. The thing I want to do is, to create a static method on a superclass, so the subclasses can be converted to XML.

Java Methods - Taking a method AS AN ARGUMENT

I've come across some code that I can't share here but it declares a method WITHIN the paramter list of another method. I didnt even know that was possible. I dont really understand why its doing that. Can someone please explain to me some possible uses that you as a programmer would have for doing that? (Note: Since I can't show the code I dont expect an in-context explanation just genera...

java - how to set default method argument values?

This question already has answers here:

java - Calling EJB in JBoss from Tomcat & passing an object as an argument

I have the following EJB class instantiated in an application running in JBoss 5 public interface ISlaveServer { public String getId(); public String getName(); } @Remote public interface IMasterServer { public String getId(); public void addSlave(ISlaveServer slaveServer); public void removeSlave(ISlaveServer slaveServer); } @Stateless @RemoteBinding(jnd...

Java method argument puzzle

I stumbled upon a very puzzling feature(?) of Java. It seems that using a "new" keyword to replace a method argument kind of shifts that object into a different scope: import java.util.ArrayList; public class Puzzle { public static void main(String[] args) { ArrayList&lt;Integer&gt; outer = new ArrayList&lt;Integer&gt;(); outer.add(17); Puzzle.change(outer); out...

How to enforce an integer limit in an argument hint in Java

I'm new to Java after working for a few years in PHP and I'm trying to get an idea of some of the best practices and norms when working with Java. I'm wondering if it makes sense to make a whole class to act as a custom type just to enforce a limit on the range of an integer argument? For example: class Person() { private int age; public Person(int age) { if(age &lt; 0) { thr...

Java -server argument

This question already has answers here:

java - The issue of * in Command line argument

I wrote a program in Java that accepts input via command line arguments. I get an input of two numbers and an operator from the command line. To multiply two numbers, I have to give input as e.g. 5 3 *, but it's not working as written. Why is it not accepting * from the command line?

java - Get Package Name of Generic Type Argument

How can I get the package name (in general, the class) of a generic type argument? I have a class similar to this: public class MyClass&lt;T&gt; {} What I'd like to do is something like this (non-working pseudo-Java): return T.class.getPackage().getName(); I need this, since JAXB needs this as the context path (JAXBContext.newInstance(String) ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)