Answer the question
In order to leave comments, you need to log in
How to correctly read and convert the contents of a text file?
Good time of the day!
Faced the problem of converting the contents of a text file. There is no need to make any changes directly to the file itself.
Print result = () -> {
try(FileReader text = new FileReader("C:\\text.txt")) {
while(( c = text.read()) != -1)
{
y += 1;
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
return 0;
}
return y;
};
char[]
, you need to know its size, I would like to create a variable with the required size. That is why I y
count the number of characters in the file in a variable. char[] a = new char[(int)result.Printer()-1];
while(( c = text.read()) != -1)
{
y += 1;
char[] a = c; Вот тут возникает ошибка
char[] a = (char)c; Так тоже не выходит
а как надо?
}
Answer the question
In order to leave comments, you need to log in
public static void main(String[] args) {
System.err.print(Arrays.toString(test()));
}
private static char[] test(){
StringBuilder sb = new StringBuilder();
try( BufferedReader br = new BufferedReader(new FileReader("D:\\text.txt"))) {
String str;
while( (str = br.readLine()) != null){
sb.append(str);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
return sb.toString().toCharArray();
}
return sb.toString().toCharArray();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question