Being a beginner in programing I'm actually writing a program to practice, wich use url.open to download pages on a forum but after 88-90 loops the url.openstream became very slow and slow down all the program's execution and i don't know why. i searched on internet the explanations but not found it.
private Set<utilisateur> arbretemp = new HashSet<utilisateur>();
private ArrayList<String> arbretempString = new ArrayList<String>();
private ArrayList<utilisateur> tempArray = new ArrayList<utilisateur>();
private String lienPartie1;
private String lienPartie2;
private int nbrePage;
private String nom;
--
String temporaire = "";
for (int i = 1; i <= nbrePage; i++) {
String line = "";
String lien = lienPartie1 + i + lienPartie2;
bar.setValue(i);
try {
url = new URL(lien);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
System.out.println("avant while");
while (((line = br.readLine()) != null)) {
System.out.println("debut de while");
if (((line.contains("alt=")) && line.contains("class=\"user-avatar-msg\""))) {
line = line.trim();
String[] temp = line.split("\"");
temporaire = temp[5] + " \r";
temp = temporaire.split("\"");
byte[] test = temporaire.getBytes();
if (!(temporaire.equalsIgnoreCase(""))) {
utilisateur utiltemp = new utilisateur(temporaire);
if (arbretemp.add(utiltemp)) {
} else {
arbretempString.add(temporaire);
}
}
}
}
System.out.println("fin de while");
} catch (MalformedURLException mue) {
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
String s = "page " + i + "/" + nbrePage;
}
System.out.println("fin de boucle");
Iterator it = arbretemp.iterator();
for (utilisateur a : arbretemp) {
// System.out.println(a.nom);
a.ajouterNbrePost(arbretempString);
}
TreeSet<utilisateur> salComp = new TreeSet<utilisateur>(new utilisateurComp());
salComp.addAll(arbretemp);
tempArray.addAll(arbretemp);
tempArray.sort(new utilisateurComp());
}
as you see it's in french but i hope you will understand anyway.
thanks for your reply and reading
Related
Am i just not initializing the AnimeItem's within the array correctly or is because of the way I'm sifting through the entire html code as one element not following the hierarchy of item, title, link, in correct order? Unsure to be honest, I tried a few different things but I can't seem to come up with a solution yet.
public static void main(String[] args)
{
String HS = "";
RSSReader r = new RSSReader();
AnimeItem[] AI = r.getItems(HS);
for(int i = 0; i < AI.length; i++)
{
System.out.println(AI[i].getENTRY() + ":\n" + AI[i].getTITLE() + "\n" + AI[i].getLINK());
}
}
public AnimeItem[] getItems(String urlAddress) {
try {
URL rssUrl = new URL(urlAddress);
BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
String line;
int ENTRYNUMBER = 0;
while ((line = in.readLine()) != null) {
if (line.contains("<item>")) {
ENTRYNUMBER++;
ANIMEITEMS[ENTRYNUMBER].setENTRY(ENTRYNUMBER);
}
if (line.contains("<title>")) {
int firstPos = line.indexOf("<title>");
String temp = line.substring(firstPos);
temp = temp.replace("<title>", "");
int lastPos = temp.indexOf("</title>");
temp = temp.substring(0, lastPos);
ANIMEITEMS[ENTRYNUMBER].setTITLE(temp);
}
if (line.contains("<link>")) {
int firstPos = line.indexOf("<link>");
String temp = line.substring(firstPos);
temp = temp.replace("<link>", "");
int lastPos = temp.indexOf("</link>");
temp = temp.substring(0, lastPos);
ANIMEITEMS[ENTRYNUMBER].setLINK(temp);
}
}
in.close();
return ANIMEITEMS;
} catch (MalformedURLException ue) {
System.out.println("Problem with URL: " + ue);
} catch (IOException ioe) {
System.out.println("Problem with IO: " + ioe);
}
System.out.println("ISSUE");
return null;
}
Sorry was being pretty dumb here, found the issue just had to increase array size far beyond what i needed kept getting array out of bounds over and over lol
copy part like this(from date to date) I am trying to copy only a part of .CSV file based on the first column (Start Date and Time) data looks like (2019-01-28 10:22:00 AM) but the user have to put it like this (2019/01/28 10:22:00)
this is for windows, java opencsv , this is what I found but dont do what I need exaclty :
like this:
int startLine = get value1 from column csv ;
int endLine = get value2 from column csv;
public static void showLines(String fileName, int startLine, int endLine) throws IOException {
String line = null;
int currentLineNo = 1;
// int startLine = 20056;//40930;
// int currentLineNo = 0;
File currentDirectory = new File(new File(".").getAbsolutePath());
String fromPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
PrintWriter pw = null;
pw = new PrintWriter(new FileOutputStream(fromPath), true);
//pw.close();
BufferedReader in = null;
try {
in = new BufferedReader (new FileReader(fileName));
//read to startLine
while(currentLineNo<startLine) {
if (in.readLine()==null) {
// oops, early end of file
throw new IOException("File too small");
}
currentLineNo++;
}
//read until endLine
while(currentLineNo<=endLine) {
line = in.readLine();
if (line==null) {
// here, we'll forgive a short file
// note finally still cleans up
return;
}
System.out.println(line);
currentLineNo++;
pw.println(line);
}
} catch (IOException ex) {
System.out.println("Problem reading file.\n" + ex.getMessage());
}finally {
try { if (in!=null) in.close();
pw.close();
} catch(IOException ignore) {}
}
}
public static void main(String[] args) throws FileNotFoundException {
int startLine = 17 ;
int endLine = 2222;
File currentDirectory = new File(new File(".").getAbsolutePath());
try {
showLines(currentDirectory.getCanonicalPath() + "\\Sources\\concat.csv", startLine, endLine);
} catch (IOException e) {
e.printStackTrace();
}
// pw.println();
}
Common CSV format uses a comma as a delimiter, with quotations used to escape any column entry that uses them within the data. Assuming that your column one data is consistent with the format you posted, and that I wouldn't have to bother with quotations marks therefor, you could read the columns as:
public static void main(String[] args) {
//This is the path to the file you are writing to
String targetPath = "";
//This is the path to the file you are reading from
String inputFilePath = "";
String line = null;
ArrayList<String> lines = new ArrayList<String>();
boolean add = false;
String startLine = "2019/01/28 10:22:00";
String endLine = "2019/01/28 10:30:00";
String addFlagSplit[] = startLine.replace("/", "-").split(" ");
String addFlag = addFlagSplit[0] + " " + addFlagSplit[1];
String endFlagSplit[] = endLine.replace("/", "-").split(" ");
String endFlag = endFlagSplit[0] + " " + endFlagSplit[1];
try(PrintWriter pw = new PrintWriter(new FileOutputStream(targetPath), true)){
try (BufferedReader input = new BufferedReader(new FileReader(inputFilePath))){
while((line = input.readLine()) != null) {
String date = line.split(",")[0];
if(date.contains(addFlag)) {
add = true;
}else if(date.contains(endFlag)) {
break;
}
if(add) {
lines.add(line);
}
}
}
for(String currentLine : lines) {
pw.append(currentLine + "\n");
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
File currentDirectory = new File(new File(".").getAbsolutePath());
String targetPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
String inputFilePath = currentDirectory.getCanonicalPath() + "\\Sources\\concat.csv";
String line = null;
ArrayList<String> lines = new ArrayList<String>();
boolean add = false;
String startLine = "2019/01/28 10:22:00";
String endLine = "2019/04/06 10:30:00";
try(PrintWriter pw = new PrintWriter(new FileOutputStream(targetPath), true)){
try (BufferedReader input = new BufferedReader(new FileReader(inputFilePath))){
while((line = input.readLine()) != null) {
String date = line.split(",")[0];
if(date.contains(startLine)) {
add = true;
}else if(date.contains(endLine)) {
break;
}
if(add) {
lines.add(line);
}
}
}
for(String currentLine : lines) {
pw.append(currentLine + "\n");
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
In all versions, i am able to get SdCard details like serial no., CID number .. except Nougot version.
I am using the below methods to get the SdCard details. Both are working fine all versions except Nougat. Those paths are returning null in nougat version
public void getCID() {
File input = new File("/sys/class/mmc_host/mmc1");
String cidDirectory = null;
File[] sid = input.listFiles();
for (int i = 0; i < sid.length; i++) {
if (sid[i].toString().contains("mmc1:")) {
cidDirectory = sid[i].toString();
String SID = (String) sid[i].toString().subSequence(cidDirectory.length() - 4, cidDirectory.length());
}
}
try {
BufferedReader CID = new BufferedReader(new FileReader(cidDirectory + "/cid"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String getSDCARDiD() {
try {
File file = new File("/sys/block/mmcblk1");
if (file.exists() && file.isDirectory()) {
memBlk = "mmcblk1";
} else {
//System.out.println("not a directory");
memBlk = "mmcblk0";
}
Process cmd = Runtime.getRuntime().exec("cat /sys/block/" + memBlk + "/device/cid");
BufferedReader br = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
sd_cid = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return sd_cid;
}
Why it is giving null i didn't understand.
Thanks in advance
Above 7.0 we have to use StorageVolume.getUuid() on StorageVolume which you get from StorageManager.
I want to create a multidimensional array and pass it as a parameter in a method and then fill the arrayList with elements and return the new version of the arrayList to be able to use that array in different classes but I get java.lang.NoSuchMethodError: I think the problem is about the way i return the array. I searched but I could not find . How can I do it correctly?
here is my code;
public test{
public static List<List<String>> 2Darray=new ArrayList<List<String>>(); // TE ERROR IN THIS LINE
public List<List<String>> fillArray(List<List<String>> array){
BufferedReader in = null;
ArrayList<String> row = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("sampleFile.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for(int i=0; i<splited.length ; i++){
row.add(splited[i]);
}
array.add(row);
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
}
return array;
}
A little tinkering (just getting it to compile) results in this which seems not to have a problem. Perhaps your issue is elsewhere.
public List<List<String>> fillArray(List<List<String>> array) {
BufferedReader in = null;
ArrayList<String> row = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("sampleFile.txt"));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for (int i = 0; i < splited.length; i++) {
row.add(splited[i]);
}
array.add(row);
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return array;
}
BTW: You should really use try with resources - it is much clearer.
Modified your code a bit so that it compiled, and replaced the reading from a text file to reading a string. There were several issues, but it seems to work. Give it a try.
The main problems I noticed were mismatching curly braces, and starting a variable name with a number.
import java.util.*;
import java.io.*;
public class Main {
public static List<List<String>> array2D = new ArrayList<List<String>>();
public List<List<String>> fillArray(List<List<String>> array) {
BufferedReader in = null;
ArrayList<String> row = new ArrayList<String>();
try {
String str = "Some test text";
InputStream is = new ByteArrayInputStream(str.getBytes());
//in = new BufferedReader(new FileReader("sampleFile.txt"));
in = new BufferedReader(new InputStreamReader(is));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("\\s+");
for(int i=0; i<splited.length ; i++) {
row.add(splited[i]);
}
array.add(row);
}
}
catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
}
finally {
try {
in.close();
}
catch (IOException e) {
}
}
return array;
}
public static void main(String[] args) {
Main main = new Main();
List<List<String>> test = main.fillArray(array2D);
for(int i = 0; i < test.size(); i++) {
for(int j = 0; j < test.get(i).size(); j++) {
System.out.println(test.get(i).get(j));
}
}
}
}
I'm completely at a lose for why this isn't working. I've had similar loops before and they've worked fine.
try{
text = new BufferedReader(new FileReader(fileName));
while ((lineOfText = text.readLine()) != null){
StringTokenizer tokens = new StringTokenizer(lineOfText," , .;:\"&!?-_\n\t12345678910[]{}()##$%^*/+-");
while (tokens.hasMoreTokens()){
countTotalWordsInDocument++;
String word = tokens.nextToken();
countTotalCharactersInAllWords = countTotalCharactersInAllWords + word.length();
}
}
text.close();
} catch (IOException e) {
System.out.println("file not found");
}