In essence, I have a method that reads words from the file and writes to an array. Sample text file:
Www Mail Login Password
WWW1 MAIL1 LOGIN1 PASSWORD1
But I want to understand what words on a new line, and which no, tell me how to do it, now my code looks like this.
bufferedreader filereader;
String BUF [] = new string [0];
{
Try {
STRING STR = NULL;
FileReader = New BufferedReader (New FilerEader ("C: \\ ... \\ SRC \\ Model \\ file.txt"));
While ((Str = FileReader.ReadLine ())! = NULL) {
// Get new words
String [] newwords = str.split (");
// Create an extended array
String [] Result = New String [BUF.LENGTH + NEWWORDS.LENGTH];
// Copy the elements in an array
System.ArrayCopy (BUF, 0, RESULT, 0, BUF.LENGTH);
System.ArrayCopy (Newwords, 0, Result, Buf.Length, NewWords.Length);
// We assign the resulting array to the current
BUF = Result;
}
FileReader.Close ();
} Catch (IoException E) {
E.PrintStackTrace ();
}
}
Answer 1
I would use the usual File and Scanner if you just need to read the rows from the text file and output them into the console. About this:
package com.company;
Import java.util.scanner;
Import java.io. *;
Public Class Main {
Public Static Void Main (String [] Args) Throws FileNotFoundException {
// below the path to the file
File MyFile = New File ("C: \\ Users \\ user \\ Projects \\ schitka \\ src \\ com \\ company \\ test.txt");
// We associate the Scanner object with our file
Scanner ScanF = New Scanner (MyFile);
STRING TEMP = "";
// While there are rows in the file
While (Scanf.HasnextLine ()) {
System.out.PrintLN (scanf.nextline ()); // or temp = scanf.nextline ();
// Temp can be divided into separators and fill in words an array or immediately use temp for something.
}
scanf.close ();
}
}
If you need to consider Russian words from the file, open the file in binary mode, and after we work with it – close using the
method
.close ();
If you need to read each character in the line
filerader fileout = null;
Try {
Filein = New FileReader ("test.txt");
int a;
While ((a = filein.read ())! = -1) {
Fileout.Write (A);
}
} finally {
if (Filein! = NULL) {
filein.close ();
}