Successfully fetched more than 100 tweets but now i am unable to store those tweets in .csv file ?
Tried for File Handling classes so how can I store the tweets?
public class SentimentAnalysisWithCount {
DoccatModel model;
static int positive = 0;
static int negative = 0;
public static void main(String[] args) throws IOException, TwitterException {
String line = "";
SentimentAnalysisWithCount twitterCategorizer = new SentimentAnalysisWithCount();
twitterCategorizer.trainModel();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("--------------------------------------------------")
.setOAuthConsumerSecret("--------------------------------------------------")
.setOAuthAccessToken("--------------------------------------------------")
.setOAuthAccessTokenSecret("--------------------------------------------------");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query("udta punjab");
QueryResult result = twitter.search(query);
int result1 = 0;
for (Status status : result.getTweets()) {
result1 = twitterCategorizer.classifyNewTweet(status.getText());
if (result1 == 1) {
positive++;
} else {
negative++;
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\User\\Desktop\\results.csv"));
bw.write("Positive Tweets," + positive);
bw.newLine();
bw.write("Negative Tweets," + negative);
bw.close();
}
public void trainModel() {
InputStream dataIn = null;
try {
dataIn = new FileInputStream("C:\\Users\\User\\Downloads\\tweets.txt");
ObjectStream lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
ObjectStream sampleStream = new DocumentSampleStream(lineStream);
// Specifies the minimum number of times a feature must be seen
int cutoff = 2;
int trainingIterations = 30;
model = DocumentCategorizerME.train("en", sampleStream, cutoff,
trainingIterations);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataIn != null) {
try {
dataIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public int classifyNewTweet(String tweet) throws IOException {
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);
double[] outcomes = myCategorizer.categorize(tweet);
String category = myCategorizer.getBestCategory(outcomes);
System.out.print("-----------------------------------------------------\nTWEET :" + tweet + " ===> ");
if (category.equalsIgnoreCase("1")) {
System.out.println(" POSITIVE ");
return 1;
} else {
System.out.println(" NEGATIVE ");
return 0;
}
}
}
In this code the tweet which is being displayed on the console that should be stored in .csv file
Please remove your API keys from Stackoverflow. You should not post them in public.
Storing tweets in a CSV is possible and you simply have to enhance your posted code-fragment by adapting the written output. The following code-snippet should give an idea on how to implement it in Java 8:
try(BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\User\\Desktop\\results.csv"))) {
int positive = 0;
int negative = 0;
StringBuilder sb = new StringBuilder();
for (Status status : result.getTweets()) {
String tweetText = status.getText();
long tweetId = status.getId();
int classificationResult = twitterCategorizer.classifyNewTweet(tweetText);
if (classificationResult == 1) {
positive++;
} else {
negative++;
}
sb.append("ID=").append(tweetId).append(",TEXT=").append(tweetText).append(",classificationResult=").append(classificationResult);
String csvText = sb.toString();
bw.write(csvText);
bw.newLine();
sb.delete(0,csvText);
}
bw.write("##### SUMMARY #####")
bw.write("Positive Tweets," + positive);
bw.newLine();
bw.write("Negative Tweets," + negative);
bw.close();
}catch(IOException e) {
//TODO Exception Handling
}
results.csv would look like:
ID=25125125,TEXT=some fancy text here,classificationResult=1
ID=25146734725,TEXT=some fancy text1 here,classificationResult=0
ID=25127575125,TEXT=some fancy text2 here,classificationResult=1
ID=251258979125,TEXT=some fancy text3 here,classificationResult=0
ID=25125867125,TEXT=some fancy text4 here,classificationResult=1
##### SUMMARY #####
Positive Tweets,3
Negative Tweets,2
Related
I have a text file (.txt) and I'm stuck here.
I user a BufferReader to read all file and save in a ArrayList then put the ArrayList into a String to remove the , [ ]
Now I need to find the word of the scanner ex: (1001) in the ArrayList that the user want, and print the line of this word and the 4 lines after that. After that, edit this 4 lines and save the ArrayList to a file.
Or have something more simple without using ArrayLists?
Thank you.
System.out.println("Digite o ID ou 1 para sair: ");
Scanner sOPFicheiro = new Scanner(System.in);
opFicheiro = sOPFicheiro.nextInt();
if (opFicheiro == 1){
System.out.println("A voltar ao menu anterior...");
Thread.sleep(1000);
editarFicheiro();
} else {
//Envia para um ArrayList o ficheiro Formandos
ArrayList<String> textoFormandos = new ArrayList<String>();
BufferedReader ler = new BufferedReader(new FileReader(FichFormandos));
String linha;
while ((linha = ler.readLine()) != null) {
textoFormandos.add(linha + "\n");
}
ler.close();
//Remove , [ ] do ArrayList para enviar para o ficheiro
String textoFormandos2 = Arrays.toString(textoFormandos.toArray()).replace("[", "").replace("]", "").replace(",", "");
}
File:
Txt File
save the ArrayList to a file
Your code lucks a mean to write data into the file.
Also invoking close() without a try/catch is a bad practice because it'll lead to resource leaks in case of exceptions.
For this task, you don't need a list, after the match with the given id is found you can write these lines to a file.
To execute this code file "test.txt" must reside under the project folder, file "result.txt" will be created automatically.
public static void main(String[] args) {
try {
readFile(Path.of("test.txt"), Path.of("result.txt"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void readFile(Path target, Path result) throws InterruptedException {
Scanner sOPFicheiro = new Scanner(System.in);
int opFicheiro = sOPFicheiro.nextInt();
if (opFicheiro == 1) {
System.out.println("A voltar ao menu anterior...");
Thread.sleep(1000);
editarFicheiro();
} else {
try (BufferedReader ler = new BufferedReader(new FileReader(target.toFile()));
BufferedWriter br = new BufferedWriter(new FileWriter(result.toFile()))) {
boolean matchIsFound = false;
String linha;
while ((linha = ler.readLine()) != null && !matchIsFound) {
if (linha.contains(String.valueOf(opFicheiro))) {
for (int i = 0; i < 5; i++) {
br.write(linha);
if (i != 4) {
br.newLine();
linha = ler.readLine();
}
}
matchIsFound = true;
}
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
user input - 1001
Initial contents of the "test.txt" file:
ID: 999
Name: ________________
Data of birth: _______
NIF: _________________
Telephone: ___________
Fim ID: 999
ID: 1001
Name: Cesar Rodrige da Silva Guimaraes
Data of birth: 16/03/2003
NIF: 1111111111
Telephone: 931111111111
Fim ID: 1001
Contents of the "result.txt" file after executing the code:
ID: 1001
Name: Cesar Rodrige da Silva Guimaraes
Data of birth: 16/03/2003
NIF: 1111111111
Telephone: 931111111111
UPDATE
public static void main(String[] args) {
try {
updateUserData(Path.of("test.txt"), Path.of("temp.txt"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void updateUserData(Path original, Path temp) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
if (id == 1) {
System.out.println("A voltar ao menu anterior...");
Thread.sleep(1000);
editarFicheiro();
return;
}
String userData = readUserData(temp, id);
String originalContentWithReplacement = readAndReplace(original, userData, id);
writeData(original, originalContentWithReplacement);
}
reading user-data for the given id from the temp file
public static String readUserData(Path temp, int id) {
StringBuilder result = new StringBuilder();
try (var reader = new BufferedReader(new FileReader(temp.toFile()))) {
boolean matchIsFound = false;
String line;
while ((line = reader.readLine()) != null && !matchIsFound) {
if (line.contains(String.valueOf(id))) {
for (int i = 0; i < 5; i++, line = reader.readLine()) {
result.append(line).append(System.getProperty("line.separator"));
}
matchIsFound = true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString().stripTrailing();
}
reading the whole contents of the original file and replacing the data for the given id
public static String readAndReplace(Path original, String userData, int id) {
StringBuilder result = new StringBuilder();
try (var reader = new BufferedReader(new FileReader(original.toFile()))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.contains(String.valueOf(id))) {
result.append(line).append(System.getProperty("line.separator"));
continue;
}
result.append(userData).append(System.getProperty("line.separator"));
for (int i = 0; i < 5; i++) {
line = reader.readLine();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString().stripTrailing();
}
replacing the previous data
public static void writeData(Path original, String content) {
try (var writer = new BufferedWriter(new FileWriter(original.toFile()))) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
Instead of using an ArrayList use a StringBuilder :
StringBuilder textoFormandos = new StringBuilder();
while ...
textoFormandos.append(linha + "\n");
...
String textoFormandos2 = textoFormandos.toString();
This way you won't need to remove anything. For the rest you need to clear the requirements.
While making a simple Client-Server GUI app where upon the user's input dimensions of shapes are read and sent back to the server,where the method drawShape is invoked,after sending the initial request("CONNECT##" + NEW) and servers response with (DIM x,y),everything stops,client receives the (DIM x,y )prints ou "1:Draw point\n2:Draw circle\n3.Draw rectangle",and THEN IT WILL NOT SEND BACK TO THE SERVER NO MATTER WHAT I TRY TO OUTPUT,(I tried with a single word) and it did not work.
I really don't know what may be the issue,and I'm struggling with it for several days.
I parsed values,closed scanner,checked scanners,checked loops...
Why is PrintWriter refusing to send OutputStream response to the server?
This is the code:
public static final int TCP_PORT = 8000;
public SGPClientThread(Socket sock) throws IOException {
this.sock = sock;
in = new BufferedReader(new InputStreamReader(sock.getInputStream()),1);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true);
start();
}
ETFCanvas can = new ETFCanvas(450, 500);
public void run() {
Scanner scan = new Scanner(System.in);
System.out.println("Send new request by entering '<NEW>'");
String option = "";
option = scan.nextLine();
out.println("CONNECT##" + option);
String read = " ";
try {
read = in.readLine();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (read.startsWith("<DIM x,y>")) {
System.out.println("1:Draw point\n2:Draw circle\n3.Draw rectangle");
// SO FAR SO GOOD!
> Following code is where the problem occurs,anything I try to print out,will not be
sent to the server,it does not have to be this,you can simply try to send a
word or something simple,not working.
**int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Dimension and color of POINT:x1,y1,color");
System.out.println("Enter X: ");
x = scan.nextInt();
System.out.println("Enter Y: ");
y = scan.nextInt();
do {
try {
System.out.println(
"Enter Color value: ETFCanvas.COLOR_RED;ETFCanvas.COLOR_BLUE;ETFCanvas.COLOR_GREEN");
color = scan.nextInt();
} catch (InputMismatchException e) {
System.out.print("Invalid input ");
}
scan.nextLine(); // clears the buffer
} while (color <= 0);
scan.close();
String iks = String.valueOf(x);
String ipsilon = String.valueOf(y);
String kolor = String.valueOf(color);
out.println("<POINT x,y,c>##" + iks + "##" + ipsilon + "##" + kolor);
break;**
Blockquote
And to keep it short I did not post the rest of the client thread it is just the Case 2 and 3 for drawing Circle and Rectangle,and closed Socket.
Here is my Server Thread code;
ETFCanvas can = new ETFCanvas(450, 500);
public ServerThread(Socket sock, int value) throws IOException {
this.sock = sock;
this.value = value;
// oos = new ObjectOutputStream(sock.getOutputStream());
// ois = new ObjectInputStream(sock.getInputStream());
in = new BufferedReader(new InputStreamReader(sock.getInputStream()),1);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true);
start();
}
#Override
public void run() {
String line = "";
//
try {
line = in.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//
if (line.startsWith("CONNECT##")) {
System.out.println("User sent request " + sock.getRemoteSocketAddress().toString() + line);
String[] content = line.split("##");
req = content[1];
if (req.equals("<NEW>")) {
out.println("<DIM x,y>");
}
} else {
System.out.println("Bad request [" + sock.getRemoteSocketAddress().toString() + "]: " + line);
> Till this part it is working like a charm,and then it will not read a clients
request for drawing
}
if (line.startsWith("<POINT x,y,c>##")) {
System.out.println("User sent request TRY" + sock.getRemoteSocketAddress().toString() + line);
String[] dim = line.split("##");
String dimX = dim[1];
String dimY = dim[2];
String dimC = dim[3];
int x = Integer.parseInt(dimX);
int y = Integer.parseInt(dimY);
int c = Integer.parseInt(dimC);
can.drawPoint(x, y, ETFCanvas.COLOR_RED);
} else if (line.startsWith("<CIRCLE x,y,r,boja>##")) {
String[] dim = line.split("##");
String dimX = dim[1];
String dimY = dim[2];
String dimR = dim[3];
String dimC = dim[4];
int x = Integer.parseInt(dimX);
int y = Integer.parseInt(dimY);
int r = Integer.parseInt(dimR);
int c = Integer.parseInt(dimC);
can.drawCircle(x, y, r, ETFCanvas.COLOR_RED);
} else if (line.startsWith("<RECTANGLE x,y,w,h,boja>##")) {
String[] dim = line.split("##");
String dimX = dim[1];
String dimY = dim[2];
String dimW = dim[3];
String dimH = dim[4];
String dimC = dim[5];
int x = Integer.parseInt(dimX);
int y = Integer.parseInt(dimY);
int w = Integer.parseInt(dimW);
int h = Integer.parseInt(dimH);
int c = Integer.parseInt(dimC);
can.drawRect(x, y, w, h, ETFCanvas.COLOR_RED);
;
try {
in.close();
out.close();
sock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
In your server code you read the first line from input but you never read next line anywhere in your code.
You should use while loop to read lines, process them and repeat:
boolean shouldProceed = true;
while (shouldProceed) {
line = in.readLine();
if (line.contains(...)) {
// do something
} else if (line.comtains(...)) {
// do something else
} else {
shouldProceed = false;
}
}
I have a text file which has text as follows:
emVersion = "1.32.4.0";
ecdbVersion = "1.8.9.6";
ReleaseVersion = "2.3.2.0";
I want to update the version number by taking the input from a user if user enter the new value for emVersion as 1.32.5.0 then
emVersion in text file will be updated as emVersion = "1.32.5.0";
All this I have to do using java code. What I have done till now is reading text file line by line then in that searching the word emVersion if found the broken line into words and then replace the token 1.32.4.0 but it is not working because spaces are unequal in the file.
Code what i have written is :
public class UpdateVariable {
public static void main(String s[]){
String replace = "1.5.6";
String UIreplace = "\""+replace+"\"";
File file =new File("C:\\Users\\310256803\\Downloads\\setup.rul");
Scanner in = null;
try {
in = new Scanner(file);
while(in.hasNext())
{
String line=in.nextLine();
if(line.contains("svEPDBVersion"))
{
String [] tokens = line.split("\\s+");
String var_1 = tokens[0];
String var_2 = tokens[1];
String var_3 = tokens[2];
String var_4 = tokens[3];
String OldVersion = var_3;
String NewVersion = UIreplace;
try{
String content = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
content = content.replaceAll(OldVersion, NewVersion);
IOUtils.write(content, new FileOutputStream(file), StandardCharsets.UTF_8);
} catch (IOException e) {
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//---this code changes each version's values but the is a option to keep the old value.
Scanner in = new Scanner(System.in);
File file = new File("versions.txt");
ArrayList<String> data = new ArrayList<>();
String[] arr =
{
"emVersion", "ecdbVersion", "releaseVersion"
};
String line = "";
String userInput = "";
try (BufferedReader br = new BufferedReader(new FileReader(file));)
{
while ((line = br.readLine()) != null)
{
data.add(line);
}
for (int i = 0; i < arr.length; i++)
{
System.out.println("Please enter new " + arr[i] + " number or (s) to keep the old value.");
userInput = in.nextLine();
line = data.get(i);
String version = line.substring(0, line.indexOf(" "));
if (arr[i].equalsIgnoreCase(version))
{
arr[i] = line.replace(line.subSequence(line.indexOf("= "), line.indexOf(";")), "= \"" + userInput + "\"");
}
if (userInput.equalsIgnoreCase("s"))
{
arr[i] = line;
}
}
PrintWriter printWriter = new PrintWriter(new FileWriter(file, false));
printWriter.println(arr[0]);
printWriter.println(arr[1]);
printWriter.println(arr[2]);
printWriter.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
Use regular expression eg:- line.trim().split("\s*=\s*"); . If it does not work please let me know , i will provide you complete solution.
I have created this code to save students info to txt file but it only saves the name. I can't find what's wrong with this any ideas? I'm not getting any errors programma runs just fine expect some error handling and other things i have to add.
Main class
public class Main {
public static void main(String[] args) throws IOException {
File fileName = new File("Students.txt");
ArrayList Students = new ArrayList();
String studentName = " ";
String studentSName = " ";
String idstudent = " ";
String course = " ";
while (!studentName.isEmpty()) {
studentName = JOptionPane.showInputDialog("Student's Name: ");
studentSName = JOptionPane.showInputDialog("Student's Surname: ");
idstudent = JOptionPane.showInputDialog("Student's IDnumber: ");
course = JOptionPane.showInputDialog("Student's Course: ");
if (!studentName.isEmpty()) {
Students.add(studentName);
}
}
try {
FileWriter fw = new FileWriter(fileName);
Writer output = new BufferedWriter(fw);
int sz = Students.size();
for (int i = 0; i < sz; i++) {
output.write(Students.get(i).toString() + "\n");
}
output.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "file not found");
}
}
}
2nd class:
public class filereading {
public static void main(String[] args) {
String FileName = "students.txt";
String line;
ArrayList Students = new ArrayList();
try {
BufferedReader input = new BufferedReader(new FileReader(FileName));
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) != null) {
Students.add(line);
}
input.close();
} catch (IOException e) {
System.out.println(e);
}
int sz = Students.size();
for (int i = 0; i < sz; i++) {
System.out.println(Students.get(i).toString());
}
}
}
Because you add only name to List
if (!studentName.isEmpty()) Students.add(studentName);
You have to add something like this:
Students.add(String.format("%s %s, id: %s, course: %s", studentName, studentSName, idstudent, course));
First text file
A.txt;
asdfghjklqw12345 qwe3456789
asdfghjklqw12345 qwe3456789
Second text file
B.txt;
|Record 1: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
|Record 2: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
Third text file
C.txt;
asdfghjklqw12345 qwe3456789 |Record 1: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
asdfghjklqw12345 qwe3456789 |Record 2: Rejected - Error on table AUTHORIZATION_TBL, column AUTH_DATE.ORA-01843: not a valid month|
for the above situation where I want to merge two lines from two different text files into one line.My code is below
List<FileInputStream> inputs = new ArrayList<FileInputStream>();
File file1 = new File("C:/Users/dell/Desktop/Test/input1.txt");
File file2 = new File("C:/Users/dell/Desktop/Test/Test.txt");
FileInputStream fis1;
FileInputStream fis2;
try {
fis1 = new FileInputStream(file1);
fis2= new FileInputStream(file2);
inputs.add(fis1);
inputs.add(fis2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int total = (int) (file1.length() + file2.length());
System.out.println("total length is " + total);
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration(inputs));
try {
System.out.println("SequenceInputStream.available() = "+ sis.available());
byte[] merge = new byte[total];
int soFar = 0;
do {
soFar += sis.read(merge,total - soFar, soFar);
} while (soFar != total);
DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:/Users/dell/Desktop/Test/C.txt"));
soFar = 0;
dos.write(merge, 0, merge.length);
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is code:
public class MergeText {
public static void main(String[] args) throws IOException{
String output="";
try(Scanner sc1=new Scanner((new File("A.txt")));
Scanner sc2=new Scanner((new File("B.txt")))){
while(sc1.hasNext() || sc2.hasNext()){
output+=sc1.next() +" "+ sc2.next();
output+="\n";
}
}
try(PrintWriter pw=new PrintWriter(new File("C.txt"))){
pw.write(output);
}
}
}
You might want to have a look at BufferedReader and BufferedWriter.
Show us what you tried and where you are stuck and we are happy to provide more help.
Merging all txt file from a folder can be done in the following way:
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<String>();
//Reading data files
try {
File folder = new File("path/inputFolder");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
BufferedReader t = new BufferedReader (new FileReader (file));
String s = null;
while ((s = t.readLine()) != null) {
list.add(s);
}
t.close();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
//Writing merged data file
BufferedWriter writer=null;
writer = new BufferedWriter(new FileWriter("data.output/merged-output.txt"));
String listWord;
for (int i = 0; i< list.size(); i++)
{
listWord = list.get(i);
writer.write(listWord);
writer.write("\n");
}
System.out.println("complited");
writer.flush();
writer.close();
}
Improved on Masudul's answer to avoid compilation errors:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MergeText {
public static void main(String[] args) throws IOException {
StringBuilder output = new StringBuilder();
try (Scanner sc1 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\A.txt")));
Scanner sc2 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\B.txt")))) {
while (sc1.hasNext() || sc2.hasNext()) {
String s1 = (sc1.hasNext() ? sc1.next() : "");
String s2 = (sc2.hasNext() ? sc2.next() : "");
output.append(s1).append(" ").append(s2);
output.append("\n");
}
}
try (PrintWriter pw = new PrintWriter(new File("C:\\Users\\mathe\\Desktop\\Fielddata\\RESULT.txt"))) {
pw.write(output.toString());
}
}
}