You really don't read with much discernment, do you? It's explicitly stated why God allowed Job to be afflicted: he did it to prove to satan that his accusations against Job were false. And Job proved him right. It wasn't until his "friends" came and accused him of sin, like you're doing, that he was pushed to the point that he lost it and found fault with God for afflicting him. And he was right, God did afflict him because he allowed it to happen. But the whole point of the story is that man, no matter how righteous (which Job was), is not in the place to judge, condemn or find fault with God for anything.
Compare Job with Moses.
Moses---Give ear, O ye heavens, and I will speak; and hear, O earth, the words of my mouth. My doctrine shall drop as the rain, my speech shall distil as the dew, as the small rain upon the tender herb, and as the showers upon the grass: Because I will publish the name of the LORD: ascribe ye greatness unto our God. He is the Rock, his work is perfect: for all his ways are judgment: a God of truth and without iniquity, just and right is he.
Job---My glory was fresh in me, and my bow was renewed in my hand. Unto me men gave ear, and waited, and kept silence at my counsel. After my words they spake not again; and my speech dropped upon them. And they waited for me as for the rain; and they opened their mouth wide as for the latter rain. If I laughed on them, they believed it not; and the light of my countenance they cast not down. I chose out their way, and sat chief, and dwelt as a king in the army, as one that comforteth the mourners.
Notice how Moses ascribed greatness to his God, while Job ascribed greatness to Job.
What Job said about God.
Job 9:17 For he breaketh me with a tempest, and multiplieth my wounds without cause.
Job 9:22 This is one thing, therefore I said it, He destroyeth the perfect and the wicked.
Job 9:23 If the scourge slay suddenly, he will laugh at the trial of the innocent.
Job 9:24 The earth is given into the hand of the wicked: he covereth the faces of the judges thereof; if not, where, and who is he?
Job 19:7-10 Behold, I cry out of wrong, but I am not heard: I cry aloud, but there is no judgment. He hath fenced up my way that I cannot pass, and he hath set darkness in my paths. He hath stripped me of my glory, and taken the crown from my head. He hath destroyed me on every side, and I am gone: and mine hope hath he removed like a tree.
What God said, Isa 45:9-10 Woe unto him that striveth with his Maker! Let the potsherd strive with the potsherds of the earth. Shall the clay say to him that fashioneth it, What makest thou? or thy work, He hath no hands? Woe unto him that saith unto his father, What begettest thou? or to the woman, What hast thou brought forth?
God said, Job 40:8 Wilt thou also disannul my judgment? wilt thou condemn me, that thou mayest be righteous?