Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to make a stream, so I can read 2 lines from .txt file to two string variables. I tried try/catch, but still have an ureported exception error.
public class Shad1 {
public void myMethod()throws FileNotFoundException {
String stringName = new String("");
String stringNumb = new String("");
File file = new File ("c:\\input.txt");
try {
DataInputStream input = new DataInputStream(new FileInputStream(file));
int check = input.read();
char data = input.readChar();
while(data != '\n') {
stringName = stringName + data;
}
while (check != -1){
stringNumb = stringNumb + data;}
input.close();
} catch (FileNotFoundException fnfe){System.out.println(fnfe.getMessage());}
}
you're using the read method: note that this method can also throw an IOException. See the docs for the read method here, the declaration is:
public final int read(byte[] b) throws IOException
So you'll also need to catch IOException, or report that your method throws IOException.
Note that you don't need to do both, so in your example code, you can similarly choose to report that your method throws FileNotFoundException or declare it in a catch block: you don't need both (unless some other part of the code in the method might generate an unhandled FileNotFoundException).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to store characters in an array of byte and write this byte array to a file and read the file back and output to the screen.
Here is my code ( I'm just starting), can anyone help me?
public static void main(String[] args) throws IOException
{
File f=new File("input.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
int c = 0;
while((c = br.read()) != -1)
{
char character = (char) c;
System.out.println(character);
I don't see here Where do you write the character array to the file input.txt?
First you have to save the data to a file and then just read it.
public static void main(String[] args) {
char [] arr = new char[] {'a', 'b', 'c'};
byte[] bytes = String.valueOf(arr).getBytes();
try(BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("input.txt"))) {
bout.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
//then part of your code
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
how to read the number of columns in text file in java.
Example text file as below. Comma separated. In this i need to get the total column as count 4
ABC,BBC,12-10-2018,1234
ABC,BBC,12-10-2018,1234
ABC,BBC,12-10-2018,1234
ABC,BBC,12-10-2018,1234
The simplest way is to use a Scanner and read the 1st line.
By using split() with , as a delimeter you get an array and its length is what you want.
public static int getFileColumnsNumber(String filename) {
File file = new File(filename);
Scanner scanner;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
}
int number = 0;
if (scanner.hasNextLine()) {
number = scanner.nextLine().split(",").length;
}
scanner.close();
return number;
}
public static void main(String[] args) {
String filename = "test.txt";
System.out.println(getFileColumnsNumber(filename));
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Suppose my text file name is "FileToUse". How do I read this file byte by byte using the read method from the FileInputStream class?
My end goal is to create a HashMap with the key as the letter (but the byte for the letter would be the key) and the value as the number of times the letter was in the text file. Any tips on how to do this would be appreciated.
Here is what you do:
First you create a file
Then, you link the file to the fileInputStream
If the file links then you assign the file contents one by one using the read() at the same time checking if it isn't the EOF. read() returns -1 for EOF
Then you process the contents into the map
File file = new File("FileToUse.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
System.out.println("Total file size to read (in bytes) : "
+ fis.available());
int content;
while ((content = fis.read()) != -1) {
// process the content into the hashmap
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there any way to print a result of a void method into a file in Java. For example, for this method below, I want to print the result in a file : file.txt not in console. Is that possible?
public void Message(String s){
System.out.Print("Your message is: "+s);
}
And thanks for answering in advance.
Here is it, but it will override the content each time :
public static void Message(String s) throws IOException {
File file = new File("put_the_path_of_your_file_here");
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(s);
fileWriter.flush();
fileWriter.close();
}
You can create a new PrintStream instance that writes to a file and use that instead of System.out, which is just a PrintStream that goes to the console.
public void Message(String s){
PrintStream p = new PrintStream("/path/to/your/file");
p.print("Your message is: " + s);
p.close();
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to read lines from an input path (file).
to do so the main calls a class that uses BufferedReader , it iterates over each line and adds it to an Array.
the problem is:
I want to catch all exceptions thrown from the method in the class in the main.
public static void main (String[] args){
if (args.length != 2){
System.err.print("ERROR");
return;
}
MyFileScript.sourceDir = args[SOURCE_DIR_INDEX];
MyFileScript.commandFile = args[COMMAND_INDEX];
try (FileReader file = new FileReader(MyFileScript.commandFile);
BufferedReader reader = new BufferedReader(file)){
fileParsing = new CommandFileParser(reader);
sectionList = fileParsing.parseFile();
}catch (FileNotFoundException error){
System.err.print(ERROR_MESSAGE);
return;
}catch(IOException error){
System.err.print(ERROR_MESSAGE);
return;
}catch(ErrorException error){
System.err.print(error.getMessage());
return;
}
}
public class CommandFileParser {
public CommandFileParser (BufferedReader reader){
this.reader = reader;
}
/**
* read all lines from a file.
*
* #return a string array containing all file lines
*/
public String[] readFileLines(){
ArrayList<String> fileLines = new ArrayList<String>();
String textLine;
while ((textLine = this.reader.readLine()) != null){
fileLines.add(textLine);
}
String[] allFileLines = new String[fileLines.size()];
fileLines.toArray(allFileLines);
return allFileLines;
}
in the while loop I get a compilation error for unhandling the IOException.
How can I catch all exceptions in main,
and so the class takes only one string argument?
your readFileLines method is lacking a throws clause.
public String[] readFileLines() throws IOException {