java.io.File getting extra char while writing into file [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Why I am getting extra char while writing into file for the following code? If I am using writeBytes(String) than the below code is working file. Then what is the problem with dos.writeChars() method?
File fileObj = new File("student.txt");
try {
// writing into file
FileOutputStream fos = new FileOutputStream(fileObj);
String msg = "This is student file";
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(msg);
//reading from file
FileInputStream fis = new FileInputStream(fileObj);
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readLine());
for (int i = 0; ((i = dis.read()) != -1); i++) {
System.out.println(i);
}
fos.close();
dos.close();
} catch (FileNotFoundException e) {
System.err.println("File not found!");
} catch (IOException e) {
e.printStackTrace();
}

writeChars() uses 2-byte chars (UTF-16). So each char you write will result in two bytes written.
If you want another encoding use getBytes() on the String and write it as bytes.

Related

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 [closed]

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
}

Why it says the file is empty? [closed]

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 3 years ago.
Improve this question
I have a method that writes integer array into a file.
When I read it with a method that brings those ints as array it's working.
But when I try to count how many ints it says it's empty.
The same code is written on other computer systems. and it's working for him, the SAME code!
I already put the "buffer" byte array into the read method. still not working
File file = new File("/home/arad/Desktop/intFile.bin");
int[] arr = {1,2,3,4,5,6};
//exampleWriteIntegerArray(arr, file);
reverseThisFile(file);
static void reverseThisFile(File file){
File newFile = new File("/home/arad/Desktop/newIntegerFile.bin");
InputStream inputStream = null;
OutputStream outputStream = null;
int counter = 0;
System.out.println(file.length());
try {
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4];
int actuallyRead;
while((actuallyRead = inputStream.read()) != -1){
counter++;``
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(counter);
}
Is something wrong in lines (input and output files are same):
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file); //comment this line
Name your variables better (outputFile, inputFile)... Also output stream outputStream is not closed... Try first just to print on console, ...
he uses Windows i use Ubuntu
I suspect this is the root of the difference, in conjunction with this code:
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file);
In the first line, you're opening the existing file to read it. In the second line, you're creating a new, empty file. Should inputStream read from that empty file, or from the file that existed before you created the new one? I believe that the behaviour of Windows and Linux (and their file systems) may differ there.
I would strongly suggest that you don't do that.
Two alternative options:
Use two different files. Read from one, write to the other.
Do all the reading first, then do all the writing.
In both of these options, you never have an input and output stream to the same file at the same time... it's that part that's the recipe for problems.

How do I read a text file using the read() method? [closed]

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();
}
}
}
}

unreported exception filenotfoundexception [closed]

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).

Scanner cannot be resolved [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
File inputTXT = new File (fileName);
try{
Scanner in = new Scanner(inputTXT);
}catch(FileNotFoundException e){
System.out.println("");
}
while(in.hasNext()){
String line = in.nextLine();
It says in can't be resolved.How am I going to fix this problem?
I've tried ignored try catch block, but this file scanner has to be in the try catch block
In your code you have issue about variable scope.You defined in variable in try catch block and then you used it in while loop.It should be like below:
File inputTXT = new File (fileName);
Scanner in=null;
try{
in = new Scanner(inputTXT);
}catch(FileNotFoundException e){
System.out.println("");
}
while(in.hasNext()){
String line = in.nextLine();

Categories